views:

54

answers:

1

I have these 2 types of Users: Parents and Kids, both are Users, and have the same properties, but different methods.

I Created Base User class and 2 class: Parent And Kid, both inherit form User base class, and each class have some different methods.
User class is partial class, because the entity framework model has the same class because the database has just Users table, no parents or kids.

Now, when i create an object from Parent class, and try to insert in the database using ado entity framework "AddObject("Users", (User)_parent)" it give me an error that i am inserting parent and that there is nothing with this identity to insert.

So, is my domain driven design has something wrong or i should edit something in the entity framework?

+2  A: 

This is called Table Per Hiearchy (TPH). It means that you want to use one table to store several entities which are inherited from base entity. In your case it is little bit specific because inherited entities don't have any additional properties to store.

You need to add Parent and Kid classes to your entity model (derived from User entity). You need one additional column in database to differ between Parent and Kid - it is the only way how EF knows which entity to create when you execute query from User table. You need to map Parent entity to proper value of discriminator column and Kid entity to other value of discriminator column. Here is a blog post about TPH in EF.

Ladislav Mrnka
perfect answer, helped me too much, and the blog post guided me step by step and everything went very well, thanks.
Amr ElGarhy