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.