Error when running Seed method using code first EF 6

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

Question

I was trying to run the update-database command to insert some initial data in my database and I got and error. Here is my class:

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

    public string Domain { get; set; }
}

and here is my Seed method:

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

After the Add-Migration AddingNotAllowedDomains, I ran Update-Database and got this 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 have an EntityTypeConfiguration class for 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();
    }
}

Reference this in your DbContext class by overriding the OnModelCreating 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