如何在实体设计器中映射每个类型的表(TPT)继承?
我在数据库中创建了大量的表,以下是问题:
Table Name -Item
ItemID - PK (Auto Increment)
Title
Table Name -Game
ItemID - PK
Console
Table Name -Film
ItemID - PK
Type
我还添加了 Item 的 ItemID 与 Film-Item 和 Game-Item 之间的关联
问题是当我将其放在 Visual Studio 中并使用实体框架似乎在某种程度上折叠了类并完全删除了电影和游戏...然后我无法在我的 ASP 代码中使用游戏或电影作为实体,有什么想法如何解决这个问题吗?
I have created a good amount of tables in my database, here are the problem ones:
Table Name -Item
ItemID - PK (Auto Increment)
Title
Table Name -Game
ItemID - PK
Console
Table Name -Film
ItemID - PK
Type
I have also added an association between the ItemID of Item and Film-Item and Game-Item
The issue is when I place this in Visual Studio and use the Entity Framework it seems to somewhat collapse the classes and totally remove Film and Game... I then cannot use Game or Film as an entity in my ASP code, any ideas how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是实体设计器的行为方式。如果您在数据库中对结构进行建模并使用从数据库更新模型,它确实会被建模为常见关联,因为 EF 还不知道您想要将其建模为继承。您将得到:
您必须手动修改此模型才能使用 TPT 继承。首先删除这两个关系。它还会删除导航属性,您将得到:
使用继承工具箱(如上一个屏幕截图中所述)并从
Film
到Item
以及从Game
到Item
绘制一条线。现在你需要完成这个模型。当前模型将不会验证,因为ItemId
已映射到父实体和子实体中。从Film
和Game
实体中删除ItemId
。您还可以将 Item 实体设为 Abstract,您将得到:Yes that is how Entity designer behaves. If you model your structure in the database and use Update model from database it will indeed be modeled as common associations beacuse EF doesn't know yet that you want to model it as inheritance. You will get this:
You must manually modify this model to use TPT inheritance. First delete both relations. It will also remove navigation properties and you will get this:
Use Inheritance from toolbox (as described on previous screenshot) and draw a line from
Film
toItem
and fromGame
toItem
. Now you need to finish this model. The current model will not validate becauseItemId
is mapped in both parent and child entity. DeleteItemId
from bothFilm
andGame
entities. You can also make Item entity Abstract and you will get this: