0

In my current solution we're using the Entity Framework (4.0). I have model that is mapped to database. In that model i have an objectType named 'Document' which is mapped to the table 'Document'.

In my solution I have the object ExtDocument which inherits from Document. ExtDocument contains some extra properties which provide extra information but don't have any influence on the existing properties of the baseclass.

My main object used throughout the application is ExtDocument. As it may happen we want to write a Document NOT ExtDocument. To our database So i thought to use the following code:

dbContext.Document.AddObject((Document) doc);

or

dbContext.Document.AddObject((doc As Document));

But in both instances an exception is thrown that there is no mapping for ExtDocument. Is there a way to write the baseclass to the database without cloning the object first?

Note 1 ExtDocument is the only type that will be deriving from Document

Note 2 I've come across many questions with a slight reference to my problem but none supplied a solution or explained why i couldn't do it (without cloning).

examples:

metadata-information-not-found-while-using-ef4s-poco-template

ef-mapping-and-metadata-information-could-not-be-found-for-entitytype-error

map-derived-type-to-the-same-table-in-ef

how-to-properly-display-or-save-inheriting-classes-into-database-with-entity-fra <= Answerless

NOTE On Duplicate:

My issue does NOT occur upon instantiating a new class of the derived type. It only occurs upon saving to database

Community
  • 1
  • 1
User999999
  • 2,500
  • 7
  • 37
  • 63
  • When the exception occurs is irrelevant, read the entire Q&A. It is simply not supported by Entity Framework to derive from entities and use those derived classes with your context, as stated in linked duplicate. Use composition instead. – CodeCaster Sep 25 '14 at 10:01
  • You still need to clone the derived object, but there is a built in function that does that. I vote to reopen. – Yuliam Chandra Sep 25 '14 at 10:20
  • @CodeCaster isn't that the "wrong use" of composition? (according to .net standards) because `ExtDocument is a Document` but `ExtDocument has no document 'as his property'`. Same as `Car is a vehicle` but a `Car has no vehichles 'as his property'`. (Although i won't deny that your solution could work) – User999999 Sep 25 '14 at 11:03
  • Depends on your domain, if it really is an "is-a" relation then yeah, it's unfortunate that you have to do an "improper" workaround because the proper way won't work. – CodeCaster Sep 25 '14 at 11:25

0 Answers0