ObjectContext.SaveChanges()违反了主键,抛出UpdateException? [英] ObjectContext.SaveChanges() violates primary key, throws UpdateException?

查看:360
本文介绍了ObjectContext.SaveChanges()违反了主键,抛出UpdateException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个MSDN文档 ...

这是INSERT语句是由产生   实体框架和执行上   当调用SaveChanges是数据源   呼吁ObjectContext的。

An INSERT statement is generated by the Entity Framework and executed on the data source when SaveChanges is called on the ObjectContext.

如果在INSERT操作成功,   服务器生成的值被写入   回到ObjectStateEntry。什么时候   的AcceptChanges时自动调用   在调用SaveChanges的端   执行时,一个永久的EntityKey是   通过使用新的计算   服务器生成的值。

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.

看起来这并不在我的code要发生。当我打电话 ObjectContext.SaveChanges(),一个 UpdateException 被抛出与 InnerException.Message 值:

This does not seem to be occurring in my code. When I call ObjectContext.SaveChanges(), an UpdateException is thrown with InnerException.Message value:

重复键值违反唯一约束student_term_data_pkey

下面是有问题的code:

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.
}

我已经验证 StudentTermData.Id 标记为我的实体数据模型中的EntityKey。没有人有任何意见或建议?

I have verified that StudentTermData.Id is marked as an EntityKey in my Entity data model. Does anyone have any ideas or suggestions?

推荐答案

此问题是由在EF4了一个错误的EF设计不设置在SSDL的StoreGeneratedPattern属性造成的。这个问题被记录在这个博客和<一href="http://connect.microsoft.com/VisualStudio/feedback/details/505178/storegeneratedpattern-property-in-ado-net-entity-model-designer-sets-cdsl-annotation-but-not-ssdl-attribute"相对=nofollow>这个Microsoft连接票。

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.

解决的办法是在文本编辑器中打开我的模型的.edml文件中找到&LT;的EntityType名称=student_term_data&GT; XML标记,并添加 StoreGeneratedPattern =身份的属性标记被用作的EntityKey:

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>

我再重新打开我的模型在EF设计,做了一个小的变化(移动实体的位置)并保存。这引起了code发电机运行,presumably同步CDSL与SSDL。

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.

我的code现在运行正常。

My code now runs as expected.

这篇关于ObjectContext.SaveChanges()违反了主键,抛出UpdateException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆