ObjectContext.SaveChanges() 违反主键,抛出 UpdateException?
摘自此 MSDN 文档 ...
INSERT 语句由以下命令生成 实体框架并执行于 SaveChanges 时的数据源 调用 ObjectContext。
如果INSERT操作成功, 写入服务器生成的值 回到ObjectStateEntry。什么时候 自动调用 AcceptChanges 在 SaveChanges 的末尾 执行时,永久的EntityKey是 通过使用新的计算 服务器生成的值。
这似乎没有发生在我的代码中。当我调用 ObjectContext.SaveChanges()
时,会抛出 UpdateException
,并带有 InnerException.Message
值:
“重复键值违反唯一约束student_term_data_pkey”
以下是有问题的代码:
using (DataAccessLayerEntities context = new DataAccessLayerEntities()) {
StudentTermData mostCurrent = new StudentTermData() {
// Foreign keys:
StudentId = studentId,
TermId = currentTerm.Id,
// Non-nullable properties:
SponsorCode = string.Empty,
AdmissionNumber = string.Empty,
Expiration = string.Empty
};
context.StudentTermDatas.AddObject(mostCurrent);
context.SaveChanges(); // UpdateException occurs here.
}
我已验证 StudentTermData.Id
在我的实体数据模型中被标记为 EntityKey。有人有什么想法或建议吗?
Paraphrasing from this MSDN documentation ...
An INSERT statement is generated by
the Entity Framework and executed on
the data source when SaveChanges is
called on the ObjectContext.If the INSERT operation succeeds,
server-generated values are written
back to the ObjectStateEntry. When
AcceptChanges is called automatically
at the end of the SaveChanges
execution, a permanent EntityKey is
computed by using the new
server-generated values.
This does not seem to be occurring in my code. When I call ObjectContext.SaveChanges()
, an UpdateException
is thrown with InnerException.Message
value:
"duplicate key value violates unique constraint student_term_data_pkey"
Here is the offending code:
using (DataAccessLayerEntities context = new DataAccessLayerEntities()) {
StudentTermData mostCurrent = new StudentTermData() {
// Foreign keys:
StudentId = studentId,
TermId = currentTerm.Id,
// Non-nullable properties:
SponsorCode = string.Empty,
AdmissionNumber = string.Empty,
Expiration = string.Empty
};
context.StudentTermDatas.AddObject(mostCurrent);
context.SaveChanges(); // UpdateException occurs here.
}
I have verified that StudentTermData.Id
is marked as an EntityKey in my Entity data model. Does anyone have any ideas or suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此问题是由 EF4 中的错误引起的,其中 EF 设计者没有在 SSDL 中设置 StoreGeneratePattern 属性。该问题记录在此博客和< a href="http://connect.microsoft.com/VisualStudio/feedback/details/505178/store generatedpattern-property-in-ado-net-entity-model-designer-sets-cdsl-annotation-but-not-ssdl-属性“rel="nofollow noreferrer">此 Microsoft Connect 票证。
解决方案是在文本编辑器中打开模型的 .edml 文件,找到
XML 标记,然后添加StoreGeneratePattern="Identity"
用作 EntityKey 的属性标签:然后,我在 EF 设计器中重新打开模型,进行一些小的更改(移动实体位置)并保存。这导致代码生成器运行,大概使 CDSL 与 SSDL 同步。
我的代码现在按预期运行。
This problem was caused by a bug in EF4 where the EF designer doesn't set the StoreGeneratedPattern attribute in the SSDL. The problem is documented in this blog and this Microsoft Connect ticket.
The solution was to open my model's .edml file in a text editor, locate the
<EntityType Name="student_term_data">
XML tag, and addStoreGeneratedPattern="Identity"
to the property tag being used as an EntityKey:I then re-opened my model in the EF designer, made a small change (moved an entity position) and saved. This caused the code generator to run, presumably synchronizing the CDSL with the SSDL.
My code now runs as expected.
编辑
如果您尝试进行插入,则不要填写 PK 字段(我猜那是 StudentID)。
EDIT
If you are trying to do an insert, then do not fill in the PK field (I'm guessing that's StudentID).