Reputation: 47
I have a Library project that I store all business logic for a Backend API service. Included in that library is the Database Context provided by Devart. How can I setup a Transient Service in my Startup.cs that will allow me to specify the connection string that I want to use when c# initiates the database with Dependency Injection? Here is some code with what I'm looking at and some of what I have tried.
I know that if you have a normal DbContext you can use the following:
_ = services.AddDbContext<DbContext>(options => options.UseSqlServer(connString));
But because this is a devart data context I am not provided the options to do that.
I have tried creating a partial class:
public partial class CustomDbContext : Library.DevartDbContext
{
public CustomDbContext() : base(dbConnectionString) { }
}
Which this is just overriding the default Devart Db Context constructor, and this works, only If I am using the CustomDbContext class in my library, which I am not.
Here is what the line looks like where I am injecting my DbContext currently and relying on the connection string to come from the library.
_ = services.AddTransient<Library.DevartDbContext>();
I have not used Ninject before, would this be a good solution that could handle this for me?
I can get my project to compile If I do this:
_ = services.AddTransient(x => new Library.DevartDbContext(dbConnectionString));
are there any consequences for setting up my db context like this? - EDIT Devart does not actually allow me to do this. I get an error with my connection string saying Key is not valid: license key. So I'm back to square 0.
Upvotes: 0
Views: 236
Reputation: 47
Turns out, my solution of using
_ = services.AddTransient(x => new Library.DevartDbContext(dbConnectionString));
was the correct solution, the errors I was getting were not related to using the Devart Data Context this way. Because I had a previous database context that is still in the project and still being used, when I updated the connection string in my startup to point to the devart one that contains a license key in it, that's where my errors were coming from.
For the time being I now just supply two different connection strings, one with the license key and one without to be used with the selective database contexts.
Upvotes: 1