Error when running Seed method using code first EF 6

ef-code-first ef-migrations entity-framework entity-framework-6

Question

I encountered an error while attempting to run the update-database command to add some initial data to my database. This is my class:

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

    public string Domain { get; set; }
}

And this is how I use seeds:

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));
}

I ran Update-Database following the Add-Migration AddingNotAllowedDomains and received the following error:

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.

Any help?

1
1
4/2/2014 4:14:58 PM

Popular Answer

Do you possess anEntityTypeConfiguration course forNotAllowedDomain ?

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();
    }
}

Include this in yourDbContext by overriding the classOnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new NotAllowedDomainConfiguration());
    base.OnModelCreating(modelBuilder);
}
0
4/3/2014 8:15:25 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow