Association and inheritance in EF
Posted: Mon 17 Oct 2011 12:53
Hello,
I’m just trying to create a big app with EF 4.2using inheritance to inject generic functionalities.
I have three classes : BaseClassA (abstract), DerivedFromA inherited from BaseClassA (table per concrete type) and ClassB.
I tried to create an association between ClassB and DerviedFromA : I can created the constraint at the store level, but at the model level, I just created an association between ClassB and BaseClassA, which is its the base class. If I try to select DerviedFromA as part of the association, ED says that there is a problem with mapping of entity key in the store.
My first idea (not shown here) is to put the entity key in an abstract top level class, having all classes inheriting from it, with some level of abstract classes : all works correctly for classes and generated tables in the store (sync is active), but I have the problem with associations, which seems to need to reference the class where the entity key is really defined (and in this case, it is the top level class!).
First question : is my config of entities wrong?
Second : is it a bad idea to use inheritance and abstract classes to simulate an objet hierarchy?
Third : is it only the ED design which is not programmed to autorize this type of configuation (which could be done directly in the xml files)?
Thanks in advance for your point of view.
Bruno
I’m just trying to create a big app with EF 4.2using inheritance to inject generic functionalities.
I have three classes : BaseClassA (abstract), DerivedFromA inherited from BaseClassA (table per concrete type) and ClassB.
I tried to create an association between ClassB and DerviedFromA : I can created the constraint at the store level, but at the model level, I just created an association between ClassB and BaseClassA, which is its the base class. If I try to select DerviedFromA as part of the association, ED says that there is a problem with mapping of entity key in the store.
My first idea (not shown here) is to put the entity key in an abstract top level class, having all classes inheriting from it, with some level of abstract classes : all works correctly for classes and generated tables in the store (sync is active), but I have the problem with associations, which seems to need to reference the class where the entity key is really defined (and in this case, it is the top level class!).
First question : is my config of entities wrong?
Second : is it a bad idea to use inheritance and abstract classes to simulate an objet hierarchy?
Third : is it only the ED design which is not programmed to autorize this type of configuation (which could be done directly in the xml files)?
Thanks in advance for your point of view.
Bruno