0

I am developing in a .net MVC web application and I am using EF(DB FIRST) to get data:

    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();
    }
}

Also using structuremap:

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

My controller:

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");
    }
}

Question:

Since I want to change the db-connectionstring at runtime how would I do this when using structuremap?

Something like:

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

How would i accomplish this?

Note: code above is not complete, I am still trying to solve this.

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • Do you have different DB contexts for the different connections or do all databases have identical structure? Not quite understanding this yet. – LocEngineer Sep 18 '18 at 14:20
  • There are several identical databases on different servers. I want to change the connection string at runtime based on what server the user wants to work with. The problem here isnt really changing the connectionstring. The problem is how do I do it when using structure map? – ThunD3eR Sep 18 '18 at 14:22
  • I just wanted to suggest something, did a quick search to confirm something and whaddayaknow: https://stackoverflow.com/questions/20216147/entity-framework-change-connection-at-runtime That seems to cover your question. Does it? – LocEngineer Sep 18 '18 at 14:27

1 Answers1

1

I guess your EF Core DbContext looks like this:

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

In this case you can just create your DbContext (you don't have to use DI everywhere) and point it to database you need:

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

Of course, you can implement somthing more sofisticated like factory class creating DbContext pointed to where you need and register this factory via DI to get it via contructor injection mechanism. The factory will have some method like:

// This is a pseudo code below
factory.CreateDbContext (.. some parameters to detect which DB to use ..)
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121