Question

I seem to be getting problems using the basic features of enable-migrations and update-database. The project I have has migrations already enabled before I copied it over to a different PC.

Am using VS 2010 Ultimate, SQL Server 2008 R2 Express, MVC 4 and EF5.

In my Configuration class I have:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

protected override void Seed(RecruitDB.ModelContext.RecruitModelContext context)
{
    context.Statuses.AddOrUpdate(
        s => s.ProgressStatus,
        new Status { ProgressStatus = "Interview Arranged" },
        new Status { ProgressStatus = "Candidate Rejected" },
        new Status { ProgressStatus = "Candidate Accepted" },
        new Status { ProgressStatus = "Job Offer Made" },
        new Status { ProgressStatus = "Job Offer Accepted" },
        new Status { ProgressStatus = "Job Offer Declined" }
        );
    // etc

In my web.config, I have EF setup to point to a database Recruitment.

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<connectionStrings>
    <add name="RecruitModelContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Initial Catalog=Recruitment;Integrated Security=True;MultipleActiveResultSets=true" />
</connectionStrings>

Why would my database get created by the name of RecruitDB.ModelContext.RecruitModelContext?

Is there some way to stop this from happening?

I try to reset by using Enable-Migrations -Force and re-writing my Configuration file after it gets overwritten.

Sometimes I get my correctly named Recruitment database and start using the application fine. Subsequently, I cannot get the Seed method to run.

I end up having to delete the database and start over.

Was it helpful?

Solution

I assume you are using EF Code First?

It seems that EF is defaulting to "Code First connection by convention", which explains why the fully qualified name of the DbContext is used as the database name. (Read more on this here: http://msdn.microsoft.com/en-us/data/jj592674)

Firstly, try to set the name of the connection string to the fully qualified name of the DbContext class, namely "RecruitDB.ModelContext.RecruitModelContext".

Should this fail, try the following steps:

  1. Drop the database RecruitDB.ModelContext.RecruitModelContext
  2. Delete all migrations in the migrations folder (Delete the entire migrations folder)
  3. Run the Enable-Migrations command
  4. Run the Add-Migration command with the ConnectionStringName parameter specified
  5. Run the Update-Database command

The following is a good EF Migrations Command Reference: http://coding.abel.nu/2012/03/ef-migrations-command-reference/#Enable-Migrations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top