Question

I have a table with an int PK, one NOT NULL field, and two NULL string fields.

When I go and set up a TPH-style design in EF, I set it up this way:

The top level type only has the PK and the NOT NULL field.

The first level checks the first nullable field as a discriminator. The not null resulting type is abstract. I map the field accordingly.

I do this again for the second field, again mapping where not null. I set nullable = false on the fields I map.

<EntitySetMapping Name="Items">
        <EntityTypeMapping TypeName="IsTypeOf(Model1.Item)">
          <MappingFragment StoreEntitySet="Items">
            <ScalarProperty Name="ID" ColumnName="ID" />
            <ScalarProperty Name="OtherID" ColumnName="OtherID" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(Model1.BothNullItem)">
          <MappingFragment StoreEntitySet="Items">
            <ScalarProperty Name="ID" ColumnName="ID" />
            <Condition ColumnName="FirstNullField" IsNull="true" />
            <Condition ColumnName="NullField2" IsNull="true" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(Model1.FirstFieldNull)">
          <MappingFragment StoreEntitySet="Items">
            <ScalarProperty Name="ID" ColumnName="ID" />
            <ScalarProperty Name=FirstNullField" ColumnName="FirstNullField" />
            <Condition ColumnName="FirstNullField" IsNull="false" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(Model1.NotNullSubItem1)">
          <MappingFragment StoreEntitySet="Items">
            <ScalarProperty Name="ID" ColumnName="ID" />
            <Condition ColumnName="NullField2" IsNull="true" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(Model1.NotNullSubItem2)">
          <MappingFragment StoreEntitySet="Items">
            <ScalarProperty Name="ID" ColumnName="ID" />
             <ScalarProperty Name="NullField2" ColumnName="NullField2" />
             <Condition ColumnName="NullField2" IsNull="false" />
          </MappingFragment>
        </EntityTypeMapping>
</EntitySetMapping>

I'm getting "two entities with different keys are mapped to the same row."

I am thinking it's because maybe the first item's not null attribute is not getting inherited by the children.

I've created a third type to try to take care of the case of NullField1 being null and NullField2 not being null (which will not happen in my DB, I have a constraint), but even if I added it it doesn't work.

Explicitly adding the not null condition on either of the 2 sub children requires me to map the column, which still doesn't work even if I map it to some extraneous property.

None of the combinations of conditions for "BothNullItem" works either.

Any ideas?

Was it helpful?

Solution

Welp, I ended up giving up on trying to get discriminator columns to propagate through inheritance so I created 3 views, one top level, one for null field1 and not null field one, and then on not null field one, I make field2 the discriminator column in EF. This sounds like a crappy answer to my problem so I'd like to hear from someone in the know what the deal is. Buehler?

UPDATE: FIXED! Going off of the horizontal partitioning concept in one of the articles I found on msdn (http://msdn.microsoft.com/en-us/library/cc716779.aspx)I went in and manually added the conditions in the msl. It looks like you can't have this work automatically from the designer. For those of you wondering, open the edmx in the xml editer and go down and look at the conditions elements of your mappings. Add in extra conditions where needed (apparently conditions don't inherit from parent entities so you have to manually add them to the children) and compile!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top