I am currently trying to implement a solution with ASP.NET Core and Oracle EF 6.
We currently have a single code base and database schema, but with 4 databases (one for each country).
I have genereated a EF Context, and created a simple partial class that implements a constructor taking a custom connection string.
public partial class MyEntities { public MyEntities (string connectionString) : base(connectionString) { } }
In my appsettings.json file I have
"ConnectionStrings": {"MyEntitiesUS": "US Connection Information","MyEntitiesFR": "France Connection Information" }
In Startup.cs ConfigureServices I have
services.AddScoped(provider => { var connectionString = Configuration["ConnectionStrings:MyEntitiesUS"]; return new CIMSEntities(connectionString); });
This works beautifully, and I can connect to the database.
Typically we have grabbed the default database to connect to from a cookie or session variable, but I don't have access to either of those values in ConfigureServices.
So my end result would look something similar to the below:
services.AddScoped(provider => { string countryCode = //grab value from cookie var connectionString = Configuration["ConnectionStrings:MyEntities" + countryCode]; return new CIMSEntities(connectionString); });
Is there a way to accomplish this, without defaulting to US and having our France users have to reconnect on ever sign-in?