Daniel Lemke
Daniel Lemke

Reputation: 2449

How do I enforce LocalDB to use the same database (across projects&entities)?

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

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:

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?

Upvotes: 0

Views: 159

Answers (1)

Daniel Lemke
Daniel Lemke

Reputation: 2449

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>

Upvotes: 0

Related Questions