[英]Table-per-type inheritance insert problem
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: 我拨打最后一个电话时收到以下错误:
"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? 关于我可能遗失的任何想法?
UPDATED WITH REPRO STEPS 更新了REPRO STEPS
Created a blank MS SQL Server 2008 R2 database, and added 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 same exceptions I described above. 我得到了与上述相同的例外情况。 Please let me know if this repro works for you, and if it does, what I'm doing wrong. 如果这个repro对你有用,请告诉我,如果有,那我做错了。 I'm blocked on this issue. 我在这个问题上被封锁了。
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). 对于那些仍然感兴趣的人,在Customers表中,Id列不应该被设置为表的标识列(虽然PK和FK依赖关系很好)。
In my case the problem was that the legacy part of our code used Map in EntityTypeConfiguration and I added a property to Item which caused the EF to split the Item entity in two tables: dbo.Item and dbo.Item1. 在我的情况下,问题是我们的代码的遗留部分使用了EntityTypeConfiguration中的Map,并且我向Item添加了一个属性,该属性导致EF将Item实体拆分为两个表:dbo.Item和dbo.Item1。 Solution was to look in the db on the properties in Item1 table and adding them to the explicit mapping. 解决方案是在Item1表中的属性中查找db并将它们添加到显式映射中。
ObjectContext.CreateObject() both creates an instance of T and simultaneously adds that instance to the ObjectContext instance from which CreateObject() was called. ObjectContext.CreateObject()都创建T的实例,同时将该实例添加到调用CreateObject()的ObjectContext实例。 So, your explicit call to AddObject() fails because a Customer object with the same key already exists. 因此,您对AddObject()的显式调用失败,因为已存在具有相同键的Customer对象。 The correct code is: 正确的代码是:
var cust = db.Users.CreateOjbect<Customer>();
// modify any properties on the customer instance
db.SaveChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.