Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,

I'm creating a model in the entity framework 6.0.

I have an abstract entity Person.
From this abstract entity I have a derived type customer.

So far common practice.

Now i want to derive an entity from the customer entity, because the model is for multiple companies, each one having specific items for their customers.

The model looking like

Person -> Customer -> CompanyX_Cyustomer

My questions are:
- Can I derive an entity from the customer entity or
- should I keep every property in the customer entity
- If I derive from the customer entity, do I have to make the customer entity an abstract type?

Kind regards

Jeroen
Posted
Comments
Suvabrata Roy 23-Jul-14 3:35am    
Are you using fluent mapping ?
Jeroen E 23-Jul-14 3:42am    
I'm sorry I don't know what that means.

I use Visual Studio 2013.
Suvabrata Roy 23-Jul-14 3:48am    
Entity Framework is a ORM (Object Relational Mapping) when ever you use any ORM you need to provide mapping for your class property to DB attributes.
EDMX file will done it behalf of you : Edmx Extension

You can use your custom provider then you required create your mapping, most of the code first case prefer own mapping tools to create mapping.
Are you using your own or EF default that is EDMX ?
Jeroen E 23-Jul-14 3:56am    
Thank you for this explanation.

I started from an empty EDMX file, so the EF default .

As per my understanding to your question, I think yes, this can be done by code but not sure whether by EDMX or not. Also this is the best way of using inheritance, and reduce redundant code.

In C# you can use any class as Base, but a class can have a single base. So you can use as like below code

C#
public class Person
{
   //Some properties & Methods for Person
}

public class Customer : Person
{
   //Some properties & Methods for Customer
}

public class CompanyX_Customer : Customer
{
   //Some properties & Methods for CompanyX_Customer
}

but you need to take care at the time of creating constructors (except default) if any.

Hope it may helpful...
 
Share this answer
 
My questions are:
- Can I derive an entity from the customer entity or

Yes you can but in edmx you need to manually write the code in designer.cs.

- should I keep every property in the customer entity

When you inherit the child class became the supper set of base class you can hide/override property but you cannot remove any property.

- If I derive from the customer entity, do I have to make the customer entity an abstract type?

Not always but it depends on your approach how you want to serve child class.

If you want to provide some basic implementation then you can use
1. virtual [^]
2. Abstract [^]

Some help for You : http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph[^]
&
Entity Framework Code First Inheritance : Table Per Hierarchy and Table Per Type[^]
&
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900