ObjectContext.SaveChanges() 违反主键,抛出 UpdateException?

发布于 2024-09-18 01:35:02 字数 1246 浏览 11 评论 0原文

摘自此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺渁∝ 2024-09-25 01:35:02

此问题是由 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 的属性标签:

  <EntityType Name="student_term_data">
     <Key><PropertyRef Name="id" /></Key>
     <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
     ...
  </EntityType>

然后,我在 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 add StoreGeneratedPattern="Identity" to the property tag being used as an EntityKey:

  <EntityType Name="student_term_data">
     <Key><PropertyRef Name="id" /></Key>
     <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
     ...
  </EntityType>

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.

滿滿的愛 2024-09-25 01:35:02

编辑

如果您尝试进行插入,则不要填写 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文