We are using Entity Framework 4.4 and using migrations. The database already exists and we need to update it on regular basis. The seed method, however, is not being called and so lookup values are not being added.
The code looks as follow:
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SqlClient", new OurSqlServerMigrationSqlGenerator());
}
protected override void Seed(KinectionDbContext context)
{
SeedLookupTables(context);
}
private static void SeedLookupTables(KinectionDbContext context)
{
context.Titles.AddOrUpdate(t => t.Value,
new Title {Value = "Mr"},
new Title {Value = "Mrs"},
new Title {Value = "Miss"},
new Title {Value = "Ms"},
new Title {Value = "Dr"}
);
context.SaveChanges();
}
}
public class MyDbContext : ObjectContext
{
public MyDbContext()
{
}
static MyDbContext ()
{
Database.SetInitializer<KinectionDbContext>(null);
}
public DbSet<Title> Titles { get; set; }
}
And we are calling:
Add-Migration Seed
But the migration comes up empty.
Does anyone has an idea why the Seed is cmot being called and why the additional values in the lookup table are not being detected?
Thanks N
The Migrations Seed method
You need to call
Update-Database
notAdd-Migration
Add-Migration
scaffolds a migration file containing commands to migrate the database to a new version. It is empty because there are no schema changes to make. You do not need to callAdd-Migration
before callingUpdate-Database
if all you want to do is seedReferences:
Code first Db initialization strategies.
Code first migrations recommended reading
Managed Migrations
Database initializer and Migrations Seed methods