简体   繁体   English

每个类型的表继承插入问题

[英]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数据库,并添加了以下两个表:

Users 用户

  • Id : bigint (PK and set it as the identity column) Id:bigint(PK并将其设置为标识列)
  • Name : nvarchar(25) NOT NULL (whatever, some random property 名称:nvarchar(25)NOT NULL(无论如何,一些随机属性

Customers 顾客

  • Id : bigint (PK, identity column, and FK on Users.Id) Id:bigint(PK,标识列和Users.Id上的FK)
  • Title : nvarchar(25) NOT NULL (also whatever, some random property) 标题:nvarchar(25)NOT NULL(也是什么,一些随机属性)

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.

相关问题 每个类型的表继承插入问题 - Table-per-type inheritance insert problem 实体框架6个自定义键,用于每种类型的表继承 - Entity framework 6 custom keys for table-per-type inheritance 在每个类型的表继承中计算查询生成 - Count Query generation in table-per-type inheritance 实体框架代码First Table-Per-Type继承包括基本类型 - Entity Framework Code First Table-Per-Type Inheritance include base type 实体框架4:插入时每个类型的表继承问题 - Entity Framework 4: Table per Type inheritance problem on insert 实体框架流利的按表类型性能替代 - Entity Framework Fluent, Table-Per-Type performance alternatives EF Table-Per-Type:将新的子代添加到现有父代 - EF Table-Per-Type: Add new child to an existing parent 每种类型的表代码优先-如何区分基本类型是子类型 - Table-per-Type Code First - How to differentiate if Base type is sub type 在使用带有实体框架的Table-per-Type时,如何仅获取基表行? - How do I get just the base table rows when using Table-per-Type with Entity Framework? 实体框架每个类型的表:1:0…*与派生类的FK关系-是基类还是派生类的FK? - Entity Framework table-per-type: 1:0…* FK relationship with a derived class - is FK of base class or derived class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM