如何在运行时使用 EF 和 Structuremap 更改连接字符串?

How to change connection string at runtime with EF and Structuremap?

我正在 .net MVC 网络应用程序中开发,我正在使用 EF(DB FIRST) 获取数据:

    public class LocationManager: MyNetworkEntities,  ILocationManager
    {
        private readonly MyNetworkEntities _dbContext = new MyNetworkEntities();

        public LocationManager(MyNetworkEntities context, string connection)
        {
            // extension for changing connectionstring at runtime
            if(connection != null)
            context.ChangeDatabase
                    (
                        dataSource: connection 
                    );


        _dbContext = context;
    }

    public List<Locations> GetAll()
    {
        return _dbContext.Locations.ToList();
    }
}

也使用结构图:

public DefaultRegistry() {
    Scan(
        scan => {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.With(new ControllerConvention());
        });
    For<ILocationManager>().Use<LocationManager>();
}

我的控制器:

public class UserController : Controller
{
    private readonly ILocationManager _locationManager;

    public UserController(ILocationManager locationManager)
    {
        _locationManager = locationManager;
    }
    // GET: User
    public ActionResult Index()
    {
        var t = _locationManager.GetAll();
        return View("UserManagment");
    }
}

问题:

因为我想在运行时更改 db-connectionstring,我该怎么做 什么时候使用结构图?

类似于:

string myNewConnection = "connection";
 var t = _locationManager.setConnection(myNewConnection).GetAll();

我将如何做到这一点?

注意:上面的代码并不完整,我还在努力解决这个问题。

我猜你的 EF Core DbContext 看起来像这样:

public MyDbContext(DbContextOptions<MyDbContext> options)
    : base(options)
{
}

在这种情况下,您只需创建 DbContext(您不必在任何地方都使用 DI)并将其指向您需要的数据库:

var connectionString = ...
var builder = new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(connectionString);
var context = new MyDbContext(builder.Options);
var locations = context.Locations.ToList();

当然,您可以实现更复杂的东西,例如工厂 class 创建指向您需要的 DbContext 并通过 DI 注册该工厂以通过构造函数注入机制获取它。工厂将有一些方法,如:

// This is a pseudo code below
factory.CreateDbContext (.. some parameters to detect which DB to use ..)