实体框架 4 - 继承
我试图理解 EF4 中的继承映射。
我的数据库有两个具有以下结构的表:
PersonCategory 表:
- CategoryID (int) (identity) (PK)
- CategoryType (nvarchar(50))
Person 表
- PersonID (int) (身份) (PK)
- CategoryID(来自 PersonCategory 表的 FK)
- 名称 (nvarchar(50))
- 描述 (nvarchar(max))
PersonCategory 表有四个条目,每个条目代表一个类别 - 学生、课程讲师、员工和顾问。
通过在线阅读文章,我认为每个层次结构表将是适合此场景的模型。因此,在 EF4 中,我创建了四个实体(学生、课程讲师、员工和顾问),每个实体都继承自 Person 表。然后,我将它们每个映射到 Person 表,并为每个添加一个条件(例如,对于学生实体,CategoryID = 1;对于员工实体,CategoryID = 2)以区别于其他实体。我还从 Person 表中删除了 CategoryID 属性,并将其设为抽象类。但我收到以下错误,因为我从 Person 表中删除了 CategoryId 属性。
错误 3015:映射从第 101、108、114、120、126、133 行开始的片段时出现问题:外键约束“FK_Person_PersonCategory”从表 Person (CategoryID) 到表 PersonCategory (CategoryID):: 映射不足:外键必须映射到概念侧参与外键关联的某些 AssociationSet 或 EntitySet。
每个层次结构表是否适合此场景?如果不是,那么我应该如何在 EF4 中处理这种情况?
I am trying to understand the inheritance mappings in EF4.
My database has two tables with the following structure:
PersonCategory Table:
- CategoryID (int) (identity) (PK)
- CategoryType (nvarchar(50))
Person Table
- PersonID (int) (identity) (PK)
- CategoryID (FK from PersonCategory table)
- Name (nvarchar(50))
- Description (nvarchar(max))
PersonCategory table has four entries each representing a category - Student, CourseInstructor, Staff and Advisor.
From reading articles online I thought Table Per Hierarchy will be a suitable model for this scenario. So in EF4, I created four entities(Student, CourseInstructor, Staff and Advisor) each inheriting from Person table. I then mapped each of them to the Person table and added a condition to each (eg. CategoryID = 1 for Student entity and CategoryID = 2 for Staff entity) to differentiate from others. I also removed the CategoryID property from Person table and made it abstract class. But I am getting the following error because I deleted the CategoryId property from Person table.
Error 3015: Problem in mapping fragments starting at lines 101, 108, 114, 120, 126, 133:Foreign key constraint 'FK_Person_PersonCategory' from table Person (CategoryID) to table PersonCategory (CategoryID):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
Is Table Per Hierarchy a suitable model for this scenario? If not then how should I approach this scenario in EF4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在关联中使用鉴别器列是有问题的。请参阅:-
http://social. msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/24380ee6-4753-46a2-a3ed-b1cb2e2d161c
“关键点是充当鉴别器的列无法映射到关联,除非它参与非空条件。”
Using discriminator columns in associations is problematic. See:-
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/24380ee6-4753-46a2-a3ed-b1cb2e2d161c
"The key point is a column that will act as discriminator cannot be mapped to an assocation unless it particpates in not null condition."
我认为你真正想要的是每种类型的表
http://www.robbagby.com/实体框架/实体框架建模-每个类型继承表/
I think what you really want is table per type
http://www.robbagby.com/entity-framework/entity-framework-modeling-table-per-type-inheritance/
对于每个层次结构表,单个表包含所有类型的所有列。你有两张桌子,所以我立刻就怀疑了。
如果选择按类型表,那么它也不正确。您只能拥有两种类型:基本类型和派生类型,PersonCategory 表提供派生类型的属性值的数据。但是,这需要将 PersonID 作为外键,但事实并非如此。
就我个人而言,我认为您不必要地将实体分割成不同的类型。您可以将两个表中的所有列映射到一个实体,或者如果可能的话,更改数据库架构以更好地满足您的需求。
我建议使用空的测试数据库并进行 TPT 和 TPH 模型优先设计,并查看 EF 为您所需的设置构建的架构。
卢克
For Table-per-Hierarchy, a single table contains all the columns for all types. You have two tables, so I'm immediately suspicious.
If going for Table-per-Type, then its not right either. You'd only be able to have two types, base and derived, with the PersonCategory table supplying the data for the derived type's property values. But, this would need to have the PersonID as the foreign key, which it doesn't.
Personally, I think you're needlessly fragmenting your entities into different types. You could map all the columns from both tables to one entity, or if possible, change the database schema to suit your needs better.
I'd suggest working with an empty test database and doing TPT and TPH model-first designs and look at the schema that EF builds for your desired setup.
Luke