Suppose that Entity Classes were generated and I wanted to expand an entity with additional fields (for example, auxiliary for calculations) that I don’t want to map.

I perform inheritance from an entity, create an instance that I fill with data.

Then I want to add an entry to the database in the Entity table and execute db.Entity.Add((Entity)ExtendedEntity) and get an error

'Object mapping could not be found for Type with identity'

Tell me how to avoid mistakes or maybe there is a more correct way?

  • How is the display configured? - Pavel Mayorov
  • @PavelMayorov, and where to look? I generated classes based on DataBase First and did not change any settings. - iluxa1810
  • If you generated classes on the basis of - from where at you could take inheritance? .. - Pavel Mayorov
  • Any class of the EF entity must have a mapping to the base, a child one is not an exception. - Pavel Mayorov
  • @PavelMayorov, Ie it turns out, if I want to add an element from the child class to the base entity, then the cast operation is not enough? Need to explicitly create a base object and transfer values ​​to it? - iluxa1810

1 answer 1

If you pass an instance of this new type to EF, then you expect it to persist (otherwise why bother with EF?). And if so, then you need to know about it EF and how to map it. In order to pick up a descendant add it immediately, the base class grab and so. About types of mapping of heirs at https://msdn.microsoft.com/en-us/data/jj591617#2 . Then your calculation fields are marked as [NotMapped] . About this at https://msdn.microsoft.com/en-us/data/jj591583#NotMapped

Good luck!