Here is the Configuration file:
internal sealed class Configuration : DbMigrationsConfiguration<Context>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Context context)
{
//my seeding DB, here's an example
context.HomePageAreas
.AddOrUpdate(new HomePageArea { Title = HomePageArea.TopAreaKey });
}
}
Application startup:
Database.SetInitializer<Context>(
new MigrateDatabaseToLatestVersion<Context, Configuration>());
using (var context = new Context())
context.Database.Initialize(false);
Then I get a DbEntityValidationException for each of the added rows (from the 2nd startup and on):
{0}: There is already a ‘{1}’ record that has its ‘{0}’ field set to ‘{2}’.
Since you are not specifying an identifier expression, EF is comparing by primary key (which does not appear in your object initializer)
Either specify the Id if it’s already known, or use a different expression. For example:
In this case, it will match an existing record by
Title.