How to skip intermediate classes in inheritance hierarchy in csdl? RRS feed

  • Question

  • Hi! Some question about inheritance mapping 

    I have three classes: class A, class B:A and class C:B. 

    I don't want class B to be mentioned in my csdl model. Only classes A and C with "direct" inheritance should be there. But any queries to ObjectSet<A> in that case return exception "Can not find object type for C". And any calls to SaveChanges() method of object context after adding instance of class C to ObjectSet<A> return exception "Can not find metadata for type C".

    More precisely - my class B is generic class and can not be used with EF directly. But class C is derived from B that is already bound to concrete type argument. Therefore class C theoretically can be mapped by EF. But I need to keep inheritance from class A in csdl in order to have common repository of base class A.
    • Changed type Fikusoid Monday, May 2, 2011 12:19 PM
    Monday, May 2, 2011 12:17 PM


All replies

  • Hello Fikusoid,


    Welcome to the EF Forum.

    As far as I know, an inheritance hierarchy as defined in CSDL must be implemented in the database tables that store data for an application built on the EDM model. There are several ways the types in an inheritance hierarchy can be structured in the database tables.

    In all implementations, the key structure of base and derived types must be identical. That is, if a Customer type is derived from the Person type, both Customer and Person types must specify the same key property or properties.

    In multi-table implementations of inheritance, the same value is assigned to the key property in more than one table to identify derived types as instances of ancestor types.

    Entity framework doesn't support intermediate non mapped types in inheritance hierarchy. It means that you can't have this inheritance: A (mapped) -> B (not mapped) -> C (mapped). EF also doesn't support mapping generic types. It means that you must remove your generic intermediate class from the hierarchy and move your Class C to derived types with correct type.


    Here is an example for How to: Define a Model with Table-per-Hierarchy Inheritance (Entity Framework).

    Another two similiar threads:





    I hope this can help you.
    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Fikusoid Friday, May 6, 2011 5:41 AM
    Wednesday, May 4, 2011 9:20 AM
  • Thanks for comprehensive answer!

    Class A in my case is only some common state used in all derived types. And derived classes don't have any additional specific state. I've decided to map with EF only class A and to remove inheritance from class A at all. Materialization of classes that were previously derived from class A is now realized in my repository code over EF. Repository injects instances of class A in classes that were previously derived from class A.

    The possibility to have "A(mapped) -> B(not mapped) -> C(mapped)" seems very useful when designing domain's models and planning to use EF as a foundation for a data access layer implementation. But I'm sure there are some deeply hidden details that prevent MS developers to realize this feature.

    Friday, May 6, 2011 5:41 AM