public partial class ProcessContext : DbContext { static ProcessContext() { Database.SetInitializer<ProcessContext>(null); } public ProcessContext() : base("Name=ProcessCS") //Comes from Config File { } --DBSets protected override void OnModelCreating(DbModelBuilder modelBuilder) { --Code } }
This is a Multi Tenent DB where we have 3 Different DB's. Centralized DB is in common location and would not be changed. This is where rest of the DB details will be stored. I need to create the Connection string @ runtime where the details will be coming from this centralized DB. Can some one please let me know how to go about it?
I tried with the following code, but it is not working. This Method will be called here
public ProcessContext() : base(nameOrConnectionString: ConnectionString()) { } private static string ConnectionString() { SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = "XXX"; sqlBuilder.InitialCatalog = "YYY"; sqlBuilder.PersistSecurityInfo = true; sqlBuilder.IntegratedSecurity = true; sqlBuilder.MultipleActiveResultSets = true; EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); entityBuilder.ProviderConnectionString = sqlBuilder.ToString(); entityBuilder.Metadata = "res://*/"; entityBuilder.Provider = "System.Data.SqlClient"; return entityBuilder.ToString(); }
If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.
SqlConnectionStringBuilder(connectionString); EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder(); ecb. Metadata = "res://*/Sample.csdl|res://*/Sample.ssdl|res://*/Sample.msl"; ecb.
The Entity Data Model tools generate a connection string that is stored in the application's configuration file.
A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.
For SQL Server connection, override the entity container class: (works in EF6, SQL server 2012 express, VS2013)
public partial class PxxxxEntities { private PxxxxEntities(string connectionString) : base(connectionString) { } public static PxxxxEntities ConnectToSqlServer(string host, string catalog, string user, string pass, bool winAuth) { SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder { DataSource = host, InitialCatalog = catalog, PersistSecurityInfo = true, IntegratedSecurity = winAuth, MultipleActiveResultSets = true, UserID = user, Password = pass, }; // assumes a connectionString name in .config of MyDbEntities var entityConnectionStringBuilder = new EntityConnectionStringBuilder { Provider = "System.Data.SqlClient", ProviderConnectionString = sqlBuilder.ConnectionString, Metadata = "res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl", }; return new PxxxxEntities(entityConnectionStringBuilder.ConnectionString); } }
You should pass an ordinary connection string into the the DbContext
constructor, not an entity connection string. So try changing your code as follows:
public ProcessContext() : base(ConnectionString()) { } private static string ConnectionString() { SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = "XXX"; sqlBuilder.InitialCatalog = "YYY"; sqlBuilder.PersistSecurityInfo = true; sqlBuilder.IntegratedSecurity = true; sqlBuilder.MultipleActiveResultSets = true; return sqlBuilder.ToString(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With