locked
EF: Associations with child entities RRS feed

  • Question

  • Is it possible to have a split entity on an inherited entity?

     

    I have two SQL tables: Base and Child.  Base has some fields in it with a PK identity.  Child has some different fields in it and a PK identity, that also has a FK relationship to the Base table's PK.  Simples?

    In EF I've inherited Child from Base, and ditched the PK in Child and set the mapping to use the Base's PK column.  Again, simples, this all works brilliantly.

    My Child class has some fields that I would like to split in my conceptual model into another entity.  So I created a new entity called, um, Sister?  And associated this with the entity Child.  I then add a key column and some fields from my Child entity to Sister (and delete them from my child entity).  The Sister entity is mapped to the Child table for the missing fields, along with the PK.

    This results in Error 3033: EntitySets 'Sister' and 'Base' are both mapped to table 'Child'.  Their primary keys may collide.

    Ok, so instead I tried making Sister inherit from Base as well (which I guess makes sense as we're splitting an inherited entity).  Again, allowing the PK column to be mapped to the Base version, and leaving the association in between Child and Sister.

    This moves me onto Error 3034: Two entities with different keys are mapped to the same row.

    I can understand the argument of the validator (working largely with BizTalk I'm used to the compiler not being sure of what's going on), but how do I force it to understand that these are the same keys?

    Thanks for any help you can offer!

    Tuesday, July 27, 2010 2:18 PM

Answers

  • Hi Alastair,

     

    Welcome to EF forum!

     

    Based on your detailed post, I can repro the exact error code (3033 and 3034) at my lab.  

     

    For the first error code, I believe the reason is:   in such structure, the ObjectContext has two ObjectSets, Bases and Sisters.   Because Child inherits Base, ObjectSet Bases will JOIN the Child table’s data.   However, the ObjectSet Sisters will also JOIN the Child table’s data because Sister rely on Child table for the Primary Keys.   Such complex relationship is not allowed in the current version of EF.  

     

    For the second error code, the relationship becomes more complicated.   Because both Child and Sister inherit Base, which means a Base entity can be in type of either Child or Sister.   However, Child and Sister split a single table Child and there is a 1: 0..1 relationship between them, which means a Sister should be also a Child.   Again, it’s not supported in EF. 

     

    In such a condition, I would recommend you not split the Child table into two entities.   Or you can consider keep the 1 : 0..1 relationship between the Base and Child tables. 

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, July 28, 2010 7:16 AM
  • Thanks for taking a look at the problem.  My knowledge of the inner workings of EF is fairly limited, your explanation clears that up nicely.

    It seems that the workable options are: (a) Do not split the table, (b) Use a database view to handle the inheritance and a proc for updates

    Wednesday, July 28, 2010 8:52 AM

All replies

  • Hi Alastair,

     

    Welcome to EF forum!

     

    Based on your detailed post, I can repro the exact error code (3033 and 3034) at my lab.  

     

    For the first error code, I believe the reason is:   in such structure, the ObjectContext has two ObjectSets, Bases and Sisters.   Because Child inherits Base, ObjectSet Bases will JOIN the Child table’s data.   However, the ObjectSet Sisters will also JOIN the Child table’s data because Sister rely on Child table for the Primary Keys.   Such complex relationship is not allowed in the current version of EF.  

     

    For the second error code, the relationship becomes more complicated.   Because both Child and Sister inherit Base, which means a Base entity can be in type of either Child or Sister.   However, Child and Sister split a single table Child and there is a 1: 0..1 relationship between them, which means a Sister should be also a Child.   Again, it’s not supported in EF. 

     

    In such a condition, I would recommend you not split the Child table into two entities.   Or you can consider keep the 1 : 0..1 relationship between the Base and Child tables. 

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, July 28, 2010 7:16 AM
  • Thanks for taking a look at the problem.  My knowledge of the inner workings of EF is fairly limited, your explanation clears that up nicely.

    It seems that the workable options are: (a) Do not split the table, (b) Use a database view to handle the inheritance and a proc for updates

    Wednesday, July 28, 2010 8:52 AM
  • Hi Alastair,

     

    You're welcome!  :-)

     

     

    Good day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 2, 2010 6:16 AM