Mapping multiple entities to one table

c# entity-framework entity-framework-6 mapping


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.

9/30/2016 1:46:25 AM

Accepted Answer

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!

9/30/2016 7:28:07 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow