私のプロジェクトでは、Asp.net Mvc、Entity Frameowrk を使用しています。私のコンテキストクラスは次のとおりです。
public class SiteContext : DbContext, IDisposable
{
public SiteContext() : base("name=SiteContext") { }
public DbSet<SystemUsers> SystemUsers { get; set; }
public DbSet<UserRoles> UserRoles { get; set; }
public DbSet<Person> Person { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SiteContext>());
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SiteContext, Configration>());
}
}
移行の私の構成クラスは次のとおりです。
public class Configration : DbMigrationsConfiguration<SiteContext>
{
public Configration()
{
AutomaticMigrationsEnabled = true; // also I changed this to false
AutomaticMigrationDataLossAllowed = true; //also I changed this to false
}
protected override void Seed(SiteContext context)
{
new List<Person>
{
new Person {Id=1, Name="admin",SurName="admin",Email="admin@admin.com",IdentityNumber="12345678900"},
}.ForEach(a => context.Person.AddOrUpdate(a));
context.SaveChanges();
}
}
移行には AddorUpdate コマンドを使用します。問題は種の部分です。Person レコードは一度も追加しません。毎回 Person レコードを追加します。どうすればこの問題を解決できますか?