我想循环遍历对象集合并将它们全部添加到表中。目标表有一个自动增量字段。如果我添加单个对象就没有问题。如果我添加两个主键都为零的对象,实体框架将失败。我可以手动指定主键,但尝试 EF 的全部目的是让生活更轻松,而不是更复杂。这是代码,收到的异常如下。
foreach (Contact contact in contacts)
{
Instructor instructor = InstructorFromContact(contact);
context.AddToInstructors(instructor);
}
try
{
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
错误是:
System.InvalidOperationException:对数据库的更改已成功提交,但更新数据库时发生错误
对象上下文。 ObjectContext 可能处于不一致的状态。
内部异常消息:AcceptChanges 无法继续,因为
对象的键值与对象中的另一个对象冲突
对象状态管理器。之前确保键值是唯一的
调用 AcceptChanges。在
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions选项)
在 System.Data.Objects.ObjectContext.SaveChanges() 处
DataMigration.Program.CopyInstructors() 中
C:\Projects\DataMigration\Program.cs:第 52 行
I want to loop through a collection of objects and add them all to a table. The destination table has an auto-increment field. If I add a single object there is no problem. If I add two objects both with the primary key of zero, the entity framework fails. I can manually specify primary keys but the whole point of trying the EF was to make life easier not more complicated. Here is the code and the exception received follows.
foreach (Contact contact in contacts)
{
Instructor instructor = InstructorFromContact(contact);
context.AddToInstructors(instructor);
}
try
{
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
The error is:
System.InvalidOperationException: The changes to the database were committed successfully, but an error occurred while updating the
object context. The ObjectContext might be in an inconsistent state.
Inner exception message: AcceptChanges cannot continue because the
object's key values conflict with another object in the
ObjectStateManager. Make sure that the key values are unique before
calling AcceptChanges. at
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges() at
DataMigration.Program.CopyInstructors() in
C:\Projects\DataMigration\Program.cs:line 52
发布评论
评论(4)
在 SSDL 中将自动增量字段的 StoreGeneratePattern 属性设置为“Identity”。应该有帮助。
Set the StoreGeneratedPattern attribute to "Identity" in your SSDL for the autoincrement field. It should help.
发生这种情况是因为尽管在数据库中创建了自动生成的列值,但 EF 从来不知道它。
因此,为了通知 EF DB 将处理生成的值,您必须打开 edmx 文件(我总是使用 VS 的 XML 编辑器来执行此操作)并在存储架构定义语言 (SSDL) 区域中添加将 StoreGeneratePattern="Identity" 属性添加到需要生成模式的列。这样EF就会读取DB中生成的值,并将其存储在内存缓存中。
您的实体类型定义或多或少看起来像这样:
请注意,如果您碰巧更新模型,所有这些更改都将丢失,并且您必须重复整个过程。
这适用于 EF 1.0,我不确定在 EF4 中所有这些问题是否都已修复。
This happens because despite the auto-generated value of the column was created in the Database the EF never knew about it.
So, in order to inform EF that the DB will handle the generated value you have to to open your edmx file (I always use the XML editor of VS to do this) and in the store schema definition language (SSDL) area, add the attribute StoreGeneratedPattern="Identity" to the column that needs the generated pattern. In this way EF reads the value generated in the DB and stores it in memory cache.
Your entity type definition will look more or less like this:
Be aware that if you happen to update your model all these changes will be lost and you'll have to repeat all the entire process.
This works for EF 1.0, I'm not sure if in EF4 all these issues are already fixed.
我使用的是 EF6,要设置 StoreGeneratePattern,您也可以尝试在 Visual Studio 中打开 EDMX 文件,右键单击表中的数据列并选择属性,
然后您可以将其从
None
设置为 <属性窗口中的 code>Identity:I'm using EF6, to set the StoreGeneratedPattern, you can also try open EDMX file in Visual Studio, right click on the Data Column in table and select Properties,
Then you can set it from
None
toIdentity
in Properties Window:可能听起来很愚蠢,但在我们的例子中,触发器中有一个不必要的 select 语句,使触发器返回数据,并且 SaveChanges() 实际上花了相当长的时间来执行,并最终引发了上述错误。
May sound silly but in our case there was an unnecessary select statement in a trigger that was making the trigger return data and the SaveChanges() actually took quite some time to execute and eventually thrown the above error.