简体   繁体   English

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

[英]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数据库:

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 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依赖关系很好)。

Why you don't want to make a foreign key (UserId) as a separate column? 为什么不想将外键(UserId)作为单独的列? Maybe it can help you. 也许可以帮到您。 Also try to use model first approach and generate db after model creation as it is described in the following article . 也尝试使用模型优先方法,并在模型创建后生成db,如以下文章中所述

声明:本站的技术帖子网页,遵循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