DotNet Core setting the connection string after startup has run

asp.net-core c# entity-framework-core

Question

I have a Visual Studio solution that has two database connections. The first is a catalog holding username password and database. The second will be the users data. I can set up the connection for the catalog database in "ConfigureServices" and thats fine. Once the user has attempted to log in and succeeded I can then know the database the user will connect to.

My problem is, How do I create the service after startup has run.. how do I use the connection string to add a DBcontext in the normal course of operations. From my searches this is OK if you know the connection string at start up..

var connection = @"Server=(localdb)\mssqllocaldb;Database=JobsLedgerDB;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));

But if I dont have a connection string at startup... How do I add a service after the project is already up and running when I finally do have the connection string?

1
2
4/26/2019 11:05:44 PM

Accepted Answer

You can instantiate your DbContext in every class of your application.

Check the docs: Configuring a DbContext

Example

var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Data Source=blog.db");

using (var context = new BloggingContext(optionsBuilder.Options))
{
  // do stuff
}

For your SQL Connection

var connection = @"Server=(localdb)\mssqllocaldb;Database=JobsLedgerDB;Trusted_Connection=True;ConnectRetryCount=0";
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlServer(connection);

using (var context = new BloggingContext(optionsBuilder.Options))
{
  // do stuff
}
0
4/26/2019 11:47:50 AM

Popular Answer

The other answer by Tolbxela suggest creating a new context when needed, where needed but that does not work if you want to use dependency injection. Instead, you should provide a factory like this in the ConfigureServices method of your Startup class using the AddDbContext extensions method as Camilo stated in the comment of that answer:

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        ...

        services.AddDbContext<BloggingContext>(options =>
        {
            var customerId = serviceProvider.GetService<IHttpContextAccessor>().HttpContext?.User?.FindFirst("customerId")?.Value;
            var connectionString = 
                $"bla blah blah ;Initial Catalog={customerId}";
            options.UseSqlServer(connectionString);

In this example the initial catalog is set to the value of a claim "customerId". (FindFirst is a custom extension method I wrote myself). This example is only to give you an idea of the approach.

You can then inject your context like you would normally do:

public class MyController : Controller
{

    public MyController(BloggingContext context)
    {
        ...
    }
};


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow