Entity Framework 6 sets the connection string in code

I have a dll that uses Entity Framework 6 to perform some database operations. I use the first approach to the database. The model and everything related to the Entity Framework, as well as the connection string in App.config, were created using wizzard in Visual Studio.

So, I compiled the dll and put it along with the corresponding .config in the folder where the application using the dll is waiting.

Everything works fine until I get to the point where the actual database call is made. There I get an error message:

Cannot find connection string for MyDatabaseEntity

The automatically generated connection string, as I said, is a DLL configuration file. I cannot change the app.config of the application. But the application passes an object that has all the information I need to build the connection string. Therefore, I am looking for a way to set the connection string in the code without relying on the configuration file. However, all the tutorials that I find for the first approach to the database use this method. I found a message here that simply says the connection string as a parameter when creating an object of type

MyDatabaseEntities = new MyDatabaseEntities(dbConnect); 

but 'MyDatabaseEntities' does not have a constructor that accepts any parameters

 public partial class MyDatabaseEntities : DbContext { public MyDatabaseEntities() : base("name=MyDatabaseEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<MyTable> MyTable { get; set; } } 
+11
source share
5 answers

What about:

 public partial class MyDatabaseEntities : DbContext { public MyDatabaseEntities(string connectionString) : base(connectionString) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<MyTable> MyTable { get; set; } 

}

Then initialize your database, as you did before:

 string myConnectionString = "..."; MyDatabaseEntities = new MyDatabaseEntities(myConnectionString); 
+16
source

I got this solution using the code below, I can hard copy the connection string using C # code without using the configuration file.

  public class SingleConnection { private SingleConnection() { } private static SingleConnection _ConsString = null; private String _String = null; public static string ConString { get { if (_ConsString == null) { _ConsString = new SingleConnection { _String = SingleConnection.Connect() }; return _ConsString._String; } else return _ConsString._String; } } public static string Connect() { //Build an SQL connection string SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder() { DataSource = "SIPL35\\SQL2016".ToString(), // Server name InitialCatalog = "Join8ShopDB", //Database UserID = "Sa", //Username Password = " Sa123!@ #", //Password }; //Build an Entity Framework connection string EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder() { Provider = "System.Data.SqlClient", Metadata = "res://*/ShopModel.csdl|res://*/ShopModel.ssdl|res://*/ShopModel.msl", ProviderConnectionString = @"data source=SIPL35\SQL2016;initial catalog=Join8ShopDB2;user id=Sa; password=Sa123!@ #;"// sqlString.ToString() }; return entityString.ConnectionString; } 

and using DbContext using the following:

 Join8ShopDBEntities dbContext = new Join8ShopDBEntities(SingleConnection.ConString); 
+4
source

I had a similar problem. My Edmx and App.Config were in another project. My launch project was different, it had 3 different connection strings, we need to choose one on the fly depending on the environment. Therefore, it was not possible to use a fixed connection string. I created a partial overload of the Context.cs class using the same namespace. Next was my default Context.cs;

 namespace CW.Repository.DBModel { public partial class CWEntities : DbContext { public CWEntities() : base("name=CWEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } ... ... } } 

My partial reload of the class;

 namespace CW.Repository.DBModel { public partial class CWEntities : DbContext { public CWEntities(string ConnectionString) : base(ConnectionString) { } } } 

Finally, since my connection strings were not for EF, I converted them to an EF connection string.

 public static string GetEntityConnectionString(string connectionString) { var entityBuilder = new EntityConnectionStringBuilder(); // WARNING // Check app config and set the appropriate DBModel entityBuilder.Provider = "System.Data.SqlClient"; entityBuilder.ProviderConnectionString = connectionString + ";MultipleActiveResultSets=True;App=EntityFramework;"; entityBuilder.Metadata = @"res://*/DBModel.CWDB.csdl|res://*/DBModel.CWDB.ssdl|res://*/DBModel.CWDB.msl"; return entityBuilder.ToString(); } 

Finally call

 var Entity = new CWEntities(CWUtilities.GetEntityConnectionString(ConnectionString)); 
+4
source

Thank you very much. I have changed a bit for Code First EF6.

 using System; using System.Collections.Generic; using System.Configuration; using System.Data.Entity.Core.EntityClient; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Data { public class SingleConnection { private SingleConnection() { } private static SingleConnection _ConsString = null; private String _String = null; public static string ConString { get { if (_ConsString == null) { _ConsString = new SingleConnection { _String = SingleConnection.Connect() }; return _ConsString._String; } else return _ConsString._String; } } public static string Connect() { string conString = ConfigurationManager.ConnectionStrings["YourConnectionStringsName"].ConnectionString; if (conString.ToLower().StartsWith("metadata=")) { System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(conString); conString = efBuilder.ProviderConnectionString; } SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString); string dataSource = cns.DataSource; SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder() { DataSource = cns.DataSource, // Server name InitialCatalog = cns.InitialCatalog, //Database UserID = cns.UserID, //Username Password = cns.Password, //Password, MultipleActiveResultSets = true, ApplicationName = "EntityFramework", }; //Build an Entity Framework connection string EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder() { Provider = "System.Data.SqlClient", Metadata = "res://*", ProviderConnectionString = sqlString.ToString() }; return entityString.ConnectionString; } } } 
0
source

You can use a singleton tongue twister for this. For instance,

 private YouurDBContext context; public YouurDBContext Context { get { if (context==null) { context = new YouurDBContext(); } return context; } set { context = value; } } 
0
source

All Articles