Deriving from classes generated by Entity Framework in C#
Asked Answered
S

2

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.

Steinbok answered 26/3, 2012 at 18:45 Comment(7)
Check out this and this SO questions.Creight
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?Wahoo
@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?Steinbok
@RaheelKhan - Sergey Sirotkin's comment seems to be your best bet.Creight
@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.Steinbok
@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.Wahoo
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.Freeness
H
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.

Hyden answered 26/3, 2012 at 21:52 Comment(5)
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.Steinbok
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.Hyden
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.Steinbok
Can you update your answer and put suggestion about composition into answer text? Currently it looks more like a comment without any proposed solution.Freeness
@SergeySirotkin: Thanks. I've marked your answer as correct. As Ladislav suggests, please do update the answer text for the benefit of others.Steinbok
F
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.

Freeness answered 26/3, 2012 at 21:44 Comment(2)
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.Steinbok
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.Freeness
H
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.

Hyden answered 26/3, 2012 at 21:52 Comment(5)
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.Steinbok
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.Hyden
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.Steinbok
Can you update your answer and put suggestion about composition into answer text? Currently it looks more like a comment without any proposed solution.Freeness
@SergeySirotkin: Thanks. I've marked your answer as correct. As Ladislav suggests, please do update the answer text for the benefit of others.Steinbok

© 2022 - 2024 — McMap. All rights reserved.