ADO.NET 实体框架:同一行可以有多个实体类型吗
我有一个由 Artist、Author 和 TextWriter 继承的基类 Participant。 我的数据存储中只有一张表: 参与者{ ID, 名, 姓, 是艺术家, 是作者, 是文本编写者, } 这个想法是为参与者可以拥有的所有角色创建一个类。
我已成功创建 edmx 文件,但当我尝试获取同时也是作者的参与者(作为艺术家)时,我收到以下错误:
EntitySet“参与者”中的所有对象都必须具有唯一的主键。但是,“Artist”类型的实例和“Author”类型的实例都具有相同的主键值“EntitySet=Participants;ID=1”。
谢谢
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.
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:
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'.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。您要求的是“每个层次结构的表”继承。您的表需要包含标识每行类型的任何“鉴别器列”。
然而,一个人的记录在具体化(从数据库中读取)时不能有多个具体类型,因为一个对象只能有一种类型。 我之前写过有关此问题的文章:
如果“Bob”是既是艺术家又是作者的
参与者
,那么他不能是以下类型,例如艺术家
和作者
同时,除非一个是另一个的超类型。艺术家和作者中的任何一个都应该与另一个具有子类型关系,或者您应该使用聚合而不是继承来将Participant
与Artist
和Author
相关联。对象的实例只能有一种具体类型;这不会改变,因为您将其存储到数据库中。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:
If "Bob" is a
Participant
who is both an Artist and an Author, then he cannot be of type, say,Artist
andAuthor
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 relateParticipant
withArtist
andAuthor
. An instance of an object can have only one concrete type; this does not change because you store it to the DB.