I would like to use EF code first with SQL Server CE 4 from an ASP.NET MVC project but instead of my database being created in the App_Data folder, I get a new database in the SQL Server Express instance installed on my workstation.
How can I get the DB created in the App_Data folder instead?
web.config:
<connectionStrings>
<add name="NerdDinners" connectionString="Data Source=|DataDirectory|NerdDinners.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Context class:
public class NerdDinners : DbContext
{
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
Edit 1
I tried stopping SQL Server Express thinking that might force the use of SQL Server CE but it resulted in a ProviderIncompatibleException with an inner exception of
{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"}
So it seems my setup wants to use SQL Server Express.
Edit 2 It was suggested that I explicitly pass the connection string name to the DbContext so I changed my context class to the following:
public class NerdDinners : DbContext
{
public NerdDinners() : base("NerdDinners") { }
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
In the DinnersController, this is how the context is instantiated:
public class DinnersController : Controller
{
private NerdDinners db = new NerdDinners();
//
// GET: /Dinners/
public ViewResult Index()
{
return View(db.Dinners.ToList());
}
After making these changes, the DB is still being created in SQL Server Express so it appears that my connection string is being ignored.