Pass the connection string to the coded first DbContext

How to pass connection string to framework-first DbContext entity? My database generation works correctly when both DbContext and the connection string in web.config are in the same project and are named the same. But now I need to move the DbContext to another project, so I am testing the transfer of the connection string to it as follows:

Model and Context

public class Dinner { public int DinnerId { get; set; } public string Title { get; set; } } public class NerdDinners : DbContext { public NerdDinners(string connString) : base(connString) { } public DbSet<Dinner> Dinners { get; set; } } 

Act

  public ActionResult Index() { var db = new NerdDinners(ConfigurationManager.ConnectionStrings["NerdDinnerDb"].ConnectionString); var dinners = (from d in db.Dinners select d).ToList(); return View(dinners); } 

Web.config

 <connectionStrings> <add name="NerdDinnerDb" connectionString="Data Source=|DataDirectory|NerdDinners.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings> 

If I set a breakpoint in action, parse the db , there will be a connection string, but it does not create or find a database or anything else.

When connecting to SQL Server, a network-related or specific instance error occurred. The server was not found or was not available. Verify the instance name is correct and configure SQL Server to connect remotely. (provider: Named Pipes provider, error: 40 - Could not open SQL Server connection)

+70
c # entity-framework database-connection
Jan 26 '11 at 13:30
source share
9 answers

After reading the documents, I should pass the connection string name instead:

 var db = new NerdDinners("NerdDinnerDb"); 
+55
Jan 26 '11 at 16:19
source share

A bit late in the game here, but another option:

 public class NerdDinners : DbContext { public NerdDinners(string connString) { this.Database.Connection.ConnectionString = connString; } public DbSet<Dinner> Dinners { get; set; } } 
+71
Aug 14 '13 at 20:15
source share

I think I’ll add this bit for people who are looking for β€œHow to pass a connection string to DbContext”: you can build a connection string for your base data store and pass the entire connection string to your type constructor from DbContext.

(Reusing code from @Lol Coder) Model and context

 public class Dinner { public int DinnerId { get; set; } public string Title { get; set; } } public class NerdDinners : DbContext { public NerdDinners(string connString) : base(connString) { } public DbSet<Dinner> Dinners { get; set; } } 

Then let's say that you are building the Sql Connection string using SqlConnectioStringBuilder, for example:

 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(GetConnectionString()); 

If the GetConnectionString method creates the corresponding connection string, and SqlConnectionStringBuilder ensures that the connection string is syntactically correct; you can then instantiate the db conetxt as follows:

 var myContext = new NerdDinners(builder.ToString()); 
+36
Oct 03 2018-11-11T00:
source share

In your DbContext, create a default constructor for your DbContext and inherit the database as follows:

  public myDbContext() : base("MyConnectionString") // connectionstring name define in your web.config { } 
+25
Nov 09 '13 at 4:50
source share

If you create a connection string in an application, you should use the connString command. If you use the connection string in web configuration. Then you use the "name" of this string.

+2
Sep 30 2018-11-11T00:
source share

Check the syntax of the connection string in the web.config file. It should be something like ConnectionString="Data Source=C:\DataDictionary\NerdDinner.sdf"

+1
Jan 26 2018-11-11T00:
source share

When using the EF model, I have a connection string in each project that uses the EF model. For example, I have an EF EDMX model in a separate class library. I have one connection string in my web (mvc) project so that it can access EF db.

I also have another unit test project for testing repositories. In order for the repositories to access db EF, the app.config file of the test project has the same connection string.

Database connections must be configured, not encoded, IMO.

+1
Jan 26 2018-11-11T00:
source share

I don’t see anything bad in your code, I use SqlExpress and it works fine when I use the connection string in the constructor.

You created the App_Data folder in your project, right?

0
Jan 26 '11 at 2:35 a.m.
source share

I have a small example of a solution to this problem.

MyDBContext.cs

  public MyDBContext(DBConnectionType ConnectionType) //: base("ConnMain") { if(ConnectionType==DBConnectionType.MainConnection) { this.Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnMain"].ConnectionString; } else if(ConnectionType==DBConnectionType.BackupConnection) { this.Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnBackup"].ConnectionString; } } 

Myclass.cs

 public enum DBConnectionType { MainConnection=0, BackupConnection=1 } 

frmMyForm.cs

  MyDBContext db = new MyDBContext(DBConnectionType.MainConnection); //or //MyDBContext db = new MyDBContext(DBConnectionType.BackupConnection); 
0
Apr 24 '18 at 16:08
source share



All Articles