Slava
Slava

Reputation: 6640

Why EF creates database file with a different name?

I was trying to do the basic database migration according Pluralsight training, however the name of the database file created doesn't match the name specified in the connection string.

<add name="DefaultConnection" 
connectionString="Data Source=(LocalDb)\v11.0; 
Initial Catalog=eManager.Web; 
Integrated Security=SSPI; 
AttachDbFilename=|DataDirectory|\eManager.mdf" providerName="System.Data.SqlClient" />

Instead of creating eManager.mdf it creates DefaultConnection.mdf

In the tutorial, it should be eManager.mdf.

Any idea why?

My Db Context is defined like this:

public class DepartmentDb : DbContext, IDepartmentDataSource
{
    public DepartmentDb() : base("DefaultConnetion")
    {
    }

    public DbSet<Employee> Employees { get; set; }
    public DbSet<Department> Departments { get; set; }

    IQueryable<Employee> IDepartmentDataSource.Employees
    {
        get { return Employees; }
    }

    IQueryable<Department> IDepartmentDataSource.Departments
    {
        get { return Departments; }
    }
}

Update: Misspelled "DefaultConnetion" in constructor param :))

Upvotes: 0

Views: 50

Answers (2)

DevilSuichiro
DevilSuichiro

Reputation: 1059

base("DefaultConnetion")

with this code, EF will look for a connection string in the app.config with this name, fail (it is called "DefaultConnection", not "DefaultConnetion"), and interpret this string as the database name.

Upvotes: 1

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

You can just put the name into your DbContext by doing something like this:

public class DataContext : DbContext
{
    public DataContext() : base("DefaultConnection")
    { }

    //....
}

Upvotes: 0

Related Questions