实体框架 4 - 继承

发布于 2024-10-01 06:07:00 字数 1112 浏览 10 评论 0原文

我试图理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-10-08 06:07:00

在关联中使用鉴别器列是有问题的。请参阅:-

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."

顾铮苏瑾 2024-10-08 06:07:00

对于每个层次结构表,单个表包含所有类型的所有列。你有两张桌子,所以我立刻就怀疑了。

如果选择按类型表,那么它也不正确。您只能拥有两种类型:基本类型和派生类型,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文