不止一个类型的实体'模型'具有相同的主键值

时间:2015-10-02 21:53:41

标签: entity-framework

我尝试插入" Person"初始化后到数据库但我遇到了这个问题:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Saving or accepting changes failed because more than one entity of type 'MyProject.Entities.Models.Person' 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.

在这一行:

private void SyncObjectsStatePreCommit()
{
foreach (var dbEntityEntry in ChangeTracker.Entries())
{
dbEntityEntry.State = StateHelper.ConvertState(((IObjectState)dbEntityEntry.Entity).ObjectState);
}
}

我如何解决这个问题 感谢

0 个答案:

没有答案