0
votes

I'm having issues to configure LocalDB properly. This is the setup:

  • Project "DataAccess", containing an EF6 DbContext with code first migrations enabled. No app.config, instead referring to the project below via StartUpProject parameter.
  • Project "Web", an MVC application that contains the following connection string:

    <add name="devDatabase" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Integrated Security=True;AttachDbFilename=c:\Temp\devDB.mdf" providerName="System.Data.SqlClient"/>

When calling Update-Database in the data project, an MDF file DataAccess.DataContext.mdf is created in the App_Data directory of the MVC project. This is the first problem, as it's not what I'd expect from the configuration above.

The second problem is that when actually starting the MVC application and accessing the database, another set of MDF files is created. In my case this will result in two more files:

  • DataAccess.DataContext`1[Entity1].mdf
  • DataAccess.DataContext`1[Entity2].mdf

So now I have my data scattered over a bunch of files, which is totally useless of course. Can anyone point me in the right direction what I'm missing here?

1

1 Answers

0
votes

Ok, after installing SQL Express and running into basically the same issue, I've found a solution. I've added the connection string name to my DbContext implementation and now it's working:

public class DataContext : DbContext
{
    public DbSet<Entity1> SomeEntities { get; set; }

    public DataContext() : base("name=DatabaseConnectionString")
    {

    }
}
<connectionStrings>
    <add name="DatabaseConnectionString" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=MBLux.CAP;Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>