views:

39

answers:

1

I have problems with saving derived type (TPT) with Entity Framework to database.

Let's say I have base entity Animal and derived type Dog.

I want to save Dog entity.

I thought that I could do it like contex.AddToDogs(), but contex contain only base entity - Animal. So I can only save Animal object - contex.AddToAnimals().

I have also tried with contex.AddObject("Animals", dogInstance), but I get the following error: The member with identity 'NavigationProperty' does not exist in the metadata collection.

But I have add EntityReference to the "NavigationProperty".

So how to save derived type in EF?

+1  A: 

The answer is contex.AddObject("Animals", dogInstance).

Originally I got error on this, but error just says NOT the navigation property name is wrong, but the Entity Set Name is wrong. And it really was. So I fix it and now saving is working properly (Dog is saved to the animal and the Dog table).

Peter Stegnar
contex.AddObject("Animals", dogInstance) gives me "The specified value is not an instance of a valid constant type\r\nParameter name: value".
Bryan