[英]Table-per-type inheritance insert problem
I followed this article on making a table-per-type inheritance model for my entities, but I ran into some issues. 我遵循了这篇文章 ,为我的实体创建了每个类型的表继承模型,但是遇到了一些问题。 Below I'm posting steps to reproduce the problem I'm having in a minimal environment to rule out other factors. 下面,我将发布一些步骤来重现我在最小环境中遇到的问题,以排除其他因素。
First, I created a new MS SQL Server 2008 R2 database with the following two tables: 首先,我使用以下两个表创建了一个新的MS SQL Server 2008 R2数据库:
Next, I generated the .edmx entity model from the database, and followed the link at the top verbatim. 接下来,我从数据库中生成了.edmx实体模型,并按原样浏览了链接。 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. 也就是说,我删除了User和Customer之间的关联,将User设置为Customer的基类,从Customer实体中删除了Id属性,并确保将Customer.Id列映射到继承的User.Id属性。 。 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 following error when I make that last call: 当我拨打最后一个电话时收到以下错误:
"A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns." “在多个位置生成了跨实体或关联共享的值。请检查映射是否不会将EntityKey拆分为多个存储生成的列。”
With the following inner exception: 有以下内部异常:
"An item with the same key has already been added." “已经添加了具有相同键的项目。”
Any ideas on what I could be missing? 关于我可能会丢失的任何想法?
A quick google on the error message turned up the following solution, maybe it helps you: 快速搜索错误消息的Google提出了以下解决方案,也许可以帮助您:
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/4bfee3fd-4124-4c1d-811d-1a5419f495d4 http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/4bfee3fd-4124-4c1d-811d-1a5419f495d4
I think that I figured it out. 我认为我已经知道了。 The table for the Party sub type had its key column set to autogenerate a key value and since it's derived, the EF wanted to set that value explicitly. Party子类型的表的键列设置为自动生成键值,并且由于该键值是派生的,因此EF希望显式设置该值。
So have you tried removing the "identity" setting from the customer table? 那么,您是否尝试过从客户表中删除“身份”设置? So it doesn't autogenerate the primary key? 因此,它不会自动生成主键吗?
Hope this helps. 希望这可以帮助。
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). 对于仍然感兴趣的用户,在“客户”表中, 不应将“ ID”列设置为表的“标识”列(不过PK和FK依赖关系很好)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.