表每类型继承插入的问题 [英] Table-per-type inheritance insert problem

查看:99
本文介绍了表每类型继承插入的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着<一个href="http://www.robbagby.com/entity-framework/entity-framework-modeling-table-per-type-inheritance/"相对=nofollow>这篇文章上制作表格,每个类型的遗传模型为我的实体,但我得到以下错误,当我试图将一个子类的实例添加到数据库中。

I followed this article on making a table-per-type inheritance model for my entities, but I get the following error when I try to add an instance of a subclass to the database.

下面是我创建的子类型:

Here is how I create the subtype:

var cust = db.Users.CreateObject<Customer>(); // Customer inherits User
db.Users.AddObject(cust);
db.SaveChanges();

我收到以下错误,当我做了最后一次通话:

I get the following error when I make that last call:

横跨在多个位置产生的。检查映射不拆分的EntityKey到多个存储生成列的实体或协会共有的价值观。

使用以下内部异常:

使用相同的密钥已被添加一个项目。

这是什么,我可能会错过任何想法?

Any ideas on what I could be missing?

更新再现步骤

创建了一个空白的MS SQL Server 2008 R2的数据库,并增加了以下两个表:

Created a blank MS SQL Server 2008 R2 database, and added the following two tables:

  • 编号:BIGINT(PK,并将其设置为标识列)
  • 名称:为nvarchar(25)NOT NULL(什么,一些随机的属性
  • 编号:BIGINT(PK,标识列和FK对Users.Id)
  • 标题:为nvarchar(25)NOT NULL(也不管,一些随机属性)

接着我生成从数据库中的.edmx实体模型,并随后在顶部逐字的链接。这就是说,我已删除的用户和客户之间的关系,设置用户作为客户的基类,删除了该客户的实体ID属性,并确保该Customer.Id列映射到继承User.Id财产。然后我跑到下面的小程序:

Next I generated the .edmx entity model from the database, and followed the link at the top verbatim. That is to say, I deleted the association between User and Customer, set User as the base class of Customer, deleted the Id property from the Customer entity, and made sure that the Customer.Id column was mapped to the inherited User.Id property. I then ran the following small program:

using (var db = new EF_Test.testEntities())
{
    var cust = db.Users.CreateObject<Customer>();
    db.Users.AddObject(cust);
    db.SaveChanges();
}

我得到我上面描述相同的异常。请让我知道如果这个摄制为你工作,如果确实如此,我在做什么错。我封锁在这个问题上。

I get the same exceptions I described above. Please let me know if this repro works for you, and if it does, what I'm doing wrong. I'm blocked on this issue.

推荐答案

我终于找到了我的烦恼​​之源。对于那些仍然有兴趣,在客户表中,ID列的不要已经被设置为表的标识列(PK和FK的依赖性​​都很好,虽然)。

I finally found the source of my troubles. For those still interested, in the Customers table, the Id column should not have been set to the identity column of the table (PK and the FK dependency are fine though).

这篇关于表每类型继承插入的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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