简体   繁体   中英

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. Creating another Customer entity in the client project throws the error saying "both types have simple names".

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.

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.

TPT (Table Per Type) is the other option where all dervied classes are mapped to their own tables beside the base class table.

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 .

Here is a tutorial how to choose EF inheritance strategy .
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.

This is a great video lessons tutorials that cover EF inheritance using code first and the designer and much much more.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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