ADO.NET实体框架:同一行可以有多种实体类型吗? [英] ADO.NET Entity Framework: Can I have multiple entity types for the same row

查看:84
本文介绍了ADO.NET实体框架:同一行可以有多种实体类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类,由Artist,Author和TextWriter继承。
我在数据存储区中只有一个表:
参与者{
ID,
FirstName,
LastName,
IsArtist,
IsAuthor,
IsTextWriter,
}
这个想法是为参与者可以拥有的所有角色提供一门课程。

I have a base class Participants inherited by Artist, Author and TextWriter. I have only one table in the data store: Participants { ID, FirstName, LastName, IsArtist, IsAuthor, IsTextWriter, } The idea is to have a class for all the roles that a participant can have.

我已经设法创建了edmx文件,但是当我尝试获得也是作者的参与者(作为艺术家)时,出现以下错误:

I've managed to create the edmx file but when I try to get an Participant (as Artist) that is also an Author I receive the following error:


EntitySet参与者中的所有对象必须具有唯一的主键。但是,类型为艺术家的实例和类型为作者的实例都具有相同的主键值 EntitySet = Participants; ID = 1。

All objects in the EntitySet 'Participants' must have unique primary keys. However, an instance of type 'Artist' and an instance of type 'Author' both have the same primary key value, 'EntitySet=Participants;ID=1'.

谢谢

推荐答案

是的,这是可能的。您要的是 每个层次结构表继承。您的表需要包含任何用于标识每一行类型的区分符列。

Yes, this is possible. What you're asking for is "table per hierarchy" inheritance. Your table needs to contain any "discriminator column" which identifies the type of each row.

但是,一个人的任何记录在实现时都不能具有一种以上的具体类型(从数据库读取),因为对象只能具有一种类型。 我之前已经写过关于这个问题的文章

However, no record for one person can have more than one concrete type when materialized (read from the DB), because an object can only have one type. I've written about this issue before:


设计好的对象关系映射时必须克服的精神障碍之一是倾向于主要以面向对象的术语或关系术语进行思考,以适合您的个性为准。但是,良好的对象关系映射同时包含良好的对象模型和良好的关系模型。例如,假设您有一个数据库,其中包含用于人员的表,以及用于员工和客户的相关表。一个人可能在所有三个表中都有一条记录。现在,从严格的关系角度来看,您可以为员工构建一个数据库VIEW,为客户构建另一个数据库,这两个数据库都合并了People表中的信息。使用一个VIEW或另一个VIEW时,即使您知道两个人都是个人,也可以暂时将一个人视为仅雇员或仅客户。因此,来自这个世界观的某个人可能会倾向于进行OO映射,其中Employee和Customer都是Person的(直接)子类。但这不适用于我们拥有的数据;由于一个人既有员工记录又有客户记录(并且由于Person实例不能同时是Employee和Customer的具体子类型),因此Person和Employee之间的OO关系需要组成而不是继承,对于Person和Customer也是如此。

One of the mental barriers that you have to get over when designing a good object relational mapping is the tendency to think primarily in object oriented terms, or relational terms, whichever suits your personality. A good object relational mapping, though, incorporates both a good object model and a good relational model. For example, let’s say you have a database with a table for People, and related tables for Employees and Customers. A single person might have a record in all three tables. Now, from a strictly relational point of view, you could construct a database VIEW for employees and another one for customers, both of which incorporate information from the People table. When using a one VIEW or the other, you can temporarily think of an individual person as "just" an Employee or "just" a Customer, even though you know that they are both. So someone coming from this worldview might be tempted to do an OO mapping where Employee and Customer are both (direct) subclasses of Person. But this doesn’t work with the data we have; since a single person has both employee and customer records (and since no Person instance can be of the concrete subtype Employee and Customer simultaneously), the OO relationship between Person and Employee needs to be composition rather than inheritance, and similarly for Person and Customer.

如果鲍勃是既是艺术家又是艺术家的参与者作者,则他不能同时是 Author 的类型,除非一个是超类型其他的。 Artist和Author之间应该有子类型的关系,或者应该使用聚合而不是继承来将参与者 Artist 和 Author 。一个对象的实例只能具有一种具体类型。这不会更改,因为您将其存储到数据库中。

If "Bob" is a Participant who is both an Artist and an Author, then he cannot be of type, say, Artist and Author at the same time, unless one is a supertype of the other. Either Artist and Author should have a subtype relationship with the other or you should use aggregation rather than inheritance to relate Participant with Artist and Author. An instance of an object can have only one concrete type; this does not change because you store it to the DB.

这篇关于ADO.NET实体框架:同一行可以有多种实体类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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