I'm trying to automatically generate my database if it doesn't exists and run the Seed()
method to populate the data. In my Database Context constructor I have this:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
This works great, my database is automatically created with all the tables as I want, but it seems like the Seed()
method is not being called, my database is empty. This is my class:
internal sealed class Configuration : DbMigrationsConfiguration<Context.MyDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
base.Seed(context);
}
}
When I run Update-Database
in the Nuget
console the data is populated after database creation, but with MigrateDatabaseToLatestVersion
the Seed()
method is not called.
What can be happening? I tried manually running migrations as taken from here:
var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);
var migrator = new DbMigrator(configuration);
migrator.Update();
But also doesn't work.
EDIT:
Ok, after some more testing I found that the Seed()
method runs but only when the database already exists, that is, on the first run when the database is created for the first time the Seed()
method is not executed, but when I run my app the second time Seed()
get's executed. I also had to add context.SaveChanges()
in order for it to work (thanks to @DavidG in the comments):
protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);
context.SaveChanges();
base.Seed(context);
}
So maybe I can manually call Seed()
inside Configuration()
and do some checking to avoid adding duplicating data or modifying data that already exists.