Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 DBContext Dynamic Connection String

Tags:

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(); } 
like image 776
Gautham Nayak Avatar asked Jan 16 '14 15:01

Gautham Nayak


People also ask

How do I change the connection string in EDMX?

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.

How do I set Entity Framework connection string programmatically?

SqlConnectionStringBuilder(connectionString); EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder(); ecb. Metadata = "res://*/Sample.csdl|res://*/Sample.ssdl|res://*/Sample.msl"; ecb.

Where is Entity Framework connection string?

The Entity Data Model tools generate a connection string that is stored in the application's configuration file.

What is EF DbContext?

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.


2 Answers

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);     } } 
like image 193
albert Avatar answered Sep 20 '22 11:09

albert


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(); } 
like image 41
luksan Avatar answered Sep 23 '22 11:09

luksan