简体   繁体   English

将多个实体映射到一个表

[英]Mapping multiple entities to one table

In my application, I have an entity, say Customer which maps to Customer. 在我的应用程序中,我有一个实体,例如“客户”,它映射到“客户”。 This is working fine now. 现在工作正常。

Our plan is to provide our current project as a reference to another client project. 我们的计划是提供我们当前的项目,以作为另一个客户项目的参考。 The client project also has a Customer table, but with some additional columns. 客户端项目也有一个Customer表,但还有一些其他列。 Creating another Customer entity in the client project throws the error saying "both types have simple names". 在客户项目中创建另一个Customer实体会引发错误,提示“两种类型都有简单的名称”。

Can this be solved using inheritance. 可以使用继承来解决。 Or does EF allow us to create classes with different names and map it to same table, like Customer and CustomerClient both maps to Customer table, but CustomerClient has the additional columns added. 还是EF允许我们创建具有不同名称的类并将其映射到同一表,例如Customer和CustomerClient都映射到Customer表,但是CustomerClient添加了其他列。

Edit: I will try to provide the code once I am in office. 编辑:我上任后将尝试提供代码。

It sounds like entity framework inheritance is what you are looking for, and especially TPH (Table Per Hierarchy) which is the the way to implement inheritance in entity frameork where all derived entities mapped to the same table. 听起来,您正在寻找实体框架继承,尤其是TPH(逐层表) ,它是在所有继承的实体映射到同一张表的实体frameork中实现继承的方法。

TPT (Table Per Type) is the other option where all dervied classes are mapped to their own tables beside the base class table. TPT(每种类型的表)是另一种选择,其中所有派生类都映射到基类表旁边的自己的表。

If you are looking for more info about implementing TPH and you are using EF code first this a good link to start with, and if you are using the designer you can use this . 如果您正在寻找有关实现TPH的更多信息,并且首先使用EF代码,那么这是一个很好的链接 ,如果您使用的是设计器,则可以使用this

Here is a tutorial how to choose EF inheritance strategy . 这是一个如何选择EF继承策略的教程。
Note: I did not mention the TPC (Table Per Concrete Class) strategy which is also described in this post becuase I do not think this is what you are looking for. 注意:我没有提到TPC(每个具体类的表)策略,在这篇文章中也有介绍,因为我不认为这是您想要的。

This is a great video lessons tutorials that cover EF inheritance using code first and the designer and much much more. 是一个很棒的视频课程教程,涵盖了首先使用代码和设计器来进行EF继承的内容。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM