5

I have created an entity data model and generated a database from it.

One of the entities is called Template.

Created partial classes to extend the functionality of Template works fine.

If I create a new class and try to derive from Template, I get a runtime exception upon instantiating:

Mapping and metadata information could not be found for EntityType 'Template001'.

How can I work around this? I definitely need to inherit from the EF classes.

EDIT

Does not seem possible. If that is the case, what would be the best way to implement the following requirement: The template entity stores information about templates that each have their own code to execute. That is why I was trying to derive from the entity in the first place.

Raheel Khan
  • 13,199
  • 12
  • 71
  • 154
  • Check out [this](http://stackoverflow.com/questions/2350514/ef-mapping-and-metadata-information-could-not-be-found-for-entitytype-error) and [this](http://stackoverflow.com/questions/2247891/metadata-information-not-found-while-using-ef4s-poco-template) SO questions. – Attila Mar 26 '12 at 18:51
  • What kind of "their own code" do you have in mind, and how should EF understand the difference between `context.Template1s.Load()` vs. `context.Template2s.Load()`, when all the database has is `Template`? Could you give an example of how you intend to use this? –  Mar 26 '12 at 22:17
  • @Attila: Thanks. The first link mentioned EF not supporting Enums which incidently I was using in the partial class but removing that did not work either. So far it seems like there is no solution except composition. Any thoughts? – Raheel Khan Mar 26 '12 at 22:33
  • @RaheelKhan - Sergey Sirotkin's comment seems to be your best bet. – Attila Mar 26 '12 at 22:40
  • @hvd: `context.Template1s.Load` will never need to be called. `Template1.DoWork()` will be called if `Template.ClassName == "Template1"`. So the entity itself knows the difference between `Template1` and `Template2`. – Raheel Khan Mar 26 '12 at 22:40
  • @RaheelKhan But that requires you to first *get* a `Template1`, right? How are you going to get it? EF will instantiate `Template` classes, because that's all the information you provided to EF. I agree with the suggestion to use the method in Sergey Sirotkin's comment. –  Mar 26 '12 at 23:00
  • The entity itself has no knowledge between Template1 or Template2 unless you hardcode all this logic to the entity directly. If this knowledge is based on inheritance and polymorphism you must map all derived classes as well. – Ladislav Mrnka Mar 27 '12 at 08:16

2 Answers2

5

It is not supported. You cannot derive a new type from entity and use it instead of the mapped entity type for persistence. If you want to have derived class from entity you must use mapped inheritance where every child is also mapped to the database.

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • I cannot map the derived classes since they are not used for persistence. They are created to implement functions depending on the underlying entity data. – Raheel Khan Mar 26 '12 at 22:10
  • No it doesn't work this way. If you derive the entity and use it either to get data from database or to save data to database it is used for persistence and you must map the derived class as well. If you can't map it, it means that your inheritance is used incorrectly and you should use composition as proposed by @Sergey. – Ladislav Mrnka Mar 27 '12 at 08:11
4

Why do you need to inherit from entity class first of all? If you want to add some simple behavior, use partial class.

Update: Based on comments, it appears that there is possibility that behavior will be extended over the time. In this case, I would recommend using composition/aggregation, not inheritance. Let the classes that need to be extended have an entity as a field. In Raheel's scenario, it would be a class called TemplateLogic with field/property of type Template.

Sergey Sirotkin
  • 1,630
  • 11
  • 7
  • The scenario is hard to explain but basically each derived class will implement custom functionality that will be added to the code base over time. The entity itself simply stores what kind of derived class should be used to process. – Raheel Khan Mar 26 '12 at 22:08
  • 4
    Then simply use composition, not inheritance. Let the classes that need to be extended have an entity as it's field. In your scenario, you would have class called TemplateLogic with field/property of type Template. – Sergey Sirotkin Mar 26 '12 at 22:29
  • Thanks. This would complicate the code a bit but will definitely work. If I don't find an answer in inheritance soon, I will end up doing this. – Raheel Khan Mar 26 '12 at 22:36
  • Can you update your answer and put suggestion about composition into answer text? Currently it looks more like a comment without any proposed solution. – Ladislav Mrnka Mar 27 '12 at 08:12
  • @SergeySirotkin: Thanks. I've marked your answer as correct. As Ladislav suggests, please do update the answer text for the benefit of others. – Raheel Khan Mar 27 '12 at 10:39