如何在实体设计器中映射每个类型的表(TPT)继承? [英] How to map Table per Type (TPT) inheritance in Entity designer?

查看:17
本文介绍了如何在实体设计器中映射每个类型的表(TPT)继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中创建了大量的表,以下是有问题的表:

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

我还添加了 Item 的 ItemID 与 Film-Item 和 Game-Item 之间的关联

I have also added an association between the ItemID of Item and Film-Item and Game-Item

问题是当我将它放在 Visual Studio 中并使用实体框架时,它似乎有点折叠类并完全删除电影和游戏......然后我不能在我的 ASP 代码中使用游戏或电影作为实体,任何想法如何解决这个问题?

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?

推荐答案

是的,这就是实体设计器的行为方式.如果您在数据库中为您的结构建模并使用从数据库更新模型,它确实会被建模为公共关联,因为 EF 还不知道您想要将其建模为继承.你会得到这个:

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:

您必须手动修改此模型以使用 TPT 继承.首先删除两个关系.它还将删除导航属性,您将获得以下信息:

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:

使用工具箱中的继承(如上一个屏幕截图所述)并从FilmItemGame<画一条线/code> 到 Item.现在你需要完成这个模型.当前模型将无法验证,因为 ItemId 在父实体和子实体中均已映射.从 FilmGame 实体中删除 ItemId.您还可以将 Item 实体设为 Abstract,您将得到:

Use Inheritance from toolbox (as described on previous screenshot) and draw a line from Film to Item and from Game to Item. Now you need to finish this model. The current model will not validate because ItemId is mapped in both parent and child entity. Delete ItemId from both Film and Game entities. You can also make Item entity Abstract and you will get this:

这篇关于如何在实体设计器中映射每个类型的表(TPT)继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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