Andre Pena
Andre Pena

Reputation: 59336

I can't specify the database name with Entity Framework 6.1.2 migrations

Here's what I did (I wanted to create a list but SO is bugged and code doesn't work after a list item for some reason):

I created a new class library for my model.

I added reference to Entity Framework and ASP.NET Identity.Created a user class.. something like this (see that I'm specifying the connection string name):

public class ApplicationUser : IdentityUser
{

}

I created an IdentityDbContext<> like this:

public class UserHubContext : IdentityDbContext<ApplicationUser>
{
    public UserHubContext() : base("DefaultConnection", throwIfV1Schema: false)
    {

    }
}

I specified the connection string like this:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=.\SQLEXPRESS;Database=MyProjectDb;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

I ran enable-migrations.

I ran add-migration initial.

I ran update-database.

Result (it's not using the database I specified):

Target database is: 'aspnet-MyProject-20150119082112' (DataSource: (LocalDb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).

That's really weird because I explicitly said the connection string should be DefraultConnection so it should use MyProjectDb as the database.

What am I missing?

Upvotes: 0

Views: 358

Answers (1)

Andre Pena
Andre Pena

Reputation: 59336

I can't believe it.

I have two projects in my solution. MyProject and MyProject.Model. Only the second had the right connection string. I was clearly running Nuget PowerShell commands on the MyProject.Model... but it was looking for the connection string on MyProject. Clearly a bug.

After I unloaded MyProject and ran the exact same commands again. Everything worked fine.

Upvotes: 2

Related Questions