none
Nested Table Inheritance RRS feed

  • Question

  • Hi,

    Imagine I have this table:

    Person

    =====

    Id (PK)

    Name

    BirthDate

    Field1

    Field2

    Field3

     

    With this entity mapped in my model, I would like to have these entities:

    Person

    =====

    Id (PK)

    Name

    BirthDate

     

    PersonA

    ======

    Field1

     

    PersonB

    ======

    Field2

    Field3

     

    If the table had a discriminator field, it could be done without any problem. Almost it can be done adding mapping conditions. But, is this inheritance possible in the way shown?

    Thanks in advance,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Friday, July 1, 2011 10:59 AM

Answers

All replies

  • Hi,

    Yes you can do this. You can use the Field1 field as a condition on PersonA and Field2 and Field3 as a condition on Person2. But this then requires that these fields is nullable in the database, since the only way to do the mapping is to check either for equality or IS NULL and IS NOT NULL.

    That should give you the entity model you want.


    --Rune
    Saturday, July 2, 2011 6:51 AM
  • Hi,

    If I set conditions Field2 and Field3 IS NULL on PersonA then I get this error:

    Problem in mapping fragments starting at line 3318:Condition member 'PersonA.Field1' with a condition other than 'IsNull=False' is mapped. Either remove the condition on PersonA.Field1 or remove it from the mapping.

    But if I add Field1 IS NOT NULL to the conditions, then I have this other error:

    Error 3032: Problem in mapping fragments starting at line 3318:Column PersonA.Field1 is used in a Not Null condition but it is mapped to a property PersonA.Field1 which is nullable. Consider making this property non-nullable.

    Any clue about how to set conditions in this model?


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Monday, July 4, 2011 7:38 AM
  • Hi!

    I can't tell you why the first error occurs, it's seems a bit strange and possible a limitation in EF. However, what you should do is on PersonA add a check on Field1 Is Not Null, and on PersonB add a condition on Field2 Is Not Null and Field3 Is Not Null. This should work.

    As for your second error. Since you are checking for the field is not null, the field can never be null on PersonA so there is no point having a nullable field in your entity and this is what the EF validation complains about. Just set the nullable property of the field to false, and it should work.

    Hope this helps!


    --Rune
    Monday, July 4, 2011 8:16 AM
  • I can't tell you why the first error occurs, it's seems a bit strange and possible a limitation in EF. However, what you should do is on PersonA add a check on Field1 Is Not Null, and on PersonB add a condition on Field2 Is Not Null and Field3 Is Not Null. This should work.


    --Rune


    Well,

    That was what I did in my previous post, and unfortunatelly this arises on first validation error.

    Then I tried to add missing conditions to each entity, but second validation error arises again.

    I think I would try to perform this scenario using classical clases...


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Monday, July 4, 2011 8:31 AM
  • From what I read of your earlier post you didn't set "Field1 Is Not Null" on PersonA and "Field2 Is Not Null And Field3 Is Not Null" on PersonB? I got the impression that you put "Field2 Is Null And Field3 Is Null" on PersonA, which isn't the same.

    In this skydrive location, I have added the .edmx file for the configuration that works for the exampel you gave, take a look at it.

    https://skydrive.live.com/redir.aspx?cid=30e30d27da569349&resid=30E30D27DA569349!142


    --Rune
    • Marked as answer by JA Reyes Monday, July 4, 2011 11:30 AM
    Monday, July 4, 2011 9:15 AM
  • Hi again,

    Well, what I did first and what I really need was that:

    1. PersonA: Field2 IS NULL And Field3 IS NULL

    2. PersonB: Field1 IS NULL

    As this gives me first validation error, then I proceed as follows:

    1. PersonA: Field2 IS NULL And Field3 IS NULL And Field1 IS NOT NULL

    2. PersonB: Field1 IS NULL And Field2 IS NOT NULL And Field3 IS NOT NULL

    But this doesn't work again...

     

    Any way, if I do this:

    1. PersonA: Field1 IS NOT NULL

    2. PersonB: Field2 IS NOT NULL And Field3 IS NOT NULL (But these fields can be both null)

    Still have second validation error...

    For what I could see in your example model, all fields are defined as Nullable="false", but I require all to be nullable...


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Monday, July 4, 2011 10:02 AM
  • Hi,

    I finally got it working in this way:

    PersonA: Field1 IS NOT NULL

    PersonB: Field1 IS NULL

    Bit in this way, I need to set Field1 property as not nullable...


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Monday, July 4, 2011 10:13 AM
  • Any way, if I do this:

    1. PersonA: Field1 IS NOT NULL

    2. PersonB: Field2 IS NOT NULL And Field3 IS NOT NULL (But these fields can be both null)

    Still have second validation error...

    For what I could see in your example model, all fields are defined as Nullable="false", but I require all to be nullable...




    Weill.. this was described in the last portion of the post earlier. The requirement for PersonA is that Field1 is not null, and then there is no point having Field1 as nullable since setting this to NULL will make the PersonA entity an ordinary Person entity. That's why you get that error.
    --Rune
    Monday, July 4, 2011 10:24 AM
  • That could also work yes!
    --Rune
    Monday, July 4, 2011 10:25 AM