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)
c # entity-framework database-connection
Shawn Mclean Jan 26 '11 at 13:30 2011-01-26 13:30
source share