使用代码首先使用EF 6运行Seed方法时出错

时间:2014-04-02 16:14:59

标签: entity-framework ef-code-first entity-framework-6 ef-migrations

我试图运行update-database命令在我的数据库中插入一些初始数据然后我得到了错误。这是我的班级:

[Table("NotAllowedDomain")]
public class NotAllowedDomain : Entity
{
    [Key]
    public int DomainId { get; set; }

    public string Domain { get; set; }
}

这是我的种子方法:

protected override void Seed(DbContext context)
{   
    var notAllowedDomainsList = new List<NotAllowedDomain>
    {
        new NotAllowedDomain {Domain = "test.com"},
        new NotAllowedDomain {Domain = "test1.com"},
        new NotAllowedDomain {Domain = "test2.co"}
    };

    notAllowedDomainsList.ForEach(x => context.NotAllowedDomains.AddOrUpdate(n => n.Domain, x));
}

Add-Migration AddingNotAllowedDomains之后,我运行了Update-Database并收到了这个错误:

Running Seed method.
System.InvalidOperationException: Saving or accepting changes failed because more than one entity of type 'Model.NotAllowedDomain' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.
   at System.Data.Entity.Core.Objects.ObjectStateManager.FixupKey(EntityEntry entry)
   at System.Data.Entity.Core.Objects.EntityEntry.AcceptChanges()
   at System.Data.Entity.Core.Objects.EntityEntry.ChangeObjectState(EntityState requestedState)
   at System.Data.Entity.Core.Objects.EntityEntry.ChangeState(EntityState state)
   at System.Data.Entity.Internal.StateEntryAdapter.ChangeState(EntityState state)
   at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)
   at System.Data.Entity.Infrastructure.DbEntityEntry.set_State(EntityState value)
   at Repository.Pattern.Ef6.DataContext.SyncObjectsStatePreCommit()
   at Repository.Pattern.Ef6.DataContext.SaveChanges()
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Saving or accepting changes failed because more than one entity of type 'Model.NotAllowedDomain' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

任何帮助?

1 个答案:

答案 0 :(得分:0)

EntityTypeConfiguration是否有NotAllowedDomain课程?

public class NotAllowedDomainConfiguration : EntityTypeConfiguration<NotAllowedDomain>
{
    public NotAllowedDomainConfiguration()
    {
        //Table
        ToTable("NotAllowedDomains");

        //Primary key
        HasKey(e => e.DomainId);

        //Properties
        Property(e => e.DomainId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(e => e.Domain).HasMaxLength(100).IsRequired();
    }
}

通过覆盖DbContext方法在您的OnModelCreating课程中引用此内容:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new NotAllowedDomainConfiguration());
    base.OnModelCreating(modelBuilder);
}