TPH Is Not NULL mapped on a relationship

As I wrote in a previous post, with EF V1, there is a limitation on the mapping of columns with the condition Is Not Null: the property mapped to the column must be scalar and can't be (indirectly) a navigation property.

So how to do the scenario of my previous post?

With a SSDL View of course:

<EntitySet Name="CustomerContacts" EntityType="TestRelationshipsOnTPHModel.Store.CustomerContacts">

    <DefiningQuery>

        SELECT ContactId, CAST(CASE WHEN CustomerId IS NULL THEN 0 ELSE 1 END AS BIT) AS IsContactCustomer

        FROM Contacts

    </DefiningQuery>

</EntitySet>

<EntityType Name="CustomerContacts">

    <Key>

        <PropertyRef Name="ContactId" />

    </Key>

    <Property Name="ContactId" Type="int" Nullable="false" />

    <Property Name="IsContactCustomer" Type="bit" />

</EntityType>

Then you map it on your derived entity type (CustomerContact). So your MSL should be this:

<edmx:Mappings>

    <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">

        <EntityContainerMapping StorageEntityContainer="TestRelationshipsOnTPHModelStoreContainer" CdmEntityContainer="TestRelationshipsOnTPHEntities">

            <EntitySetMapping Name="Contacts">

                <EntityTypeMapping TypeName="IsTypeOf(TestRelationshipsOnTPHModel.Contact)">

                    <MappingFragment StoreEntitySet="Contacts">

                        <ScalarProperty Name="ContactName" ColumnName="ContactName" />

                        <ScalarProperty Name="ContactId" ColumnName="ContactId" />

                    </MappingFragment>

                </EntityTypeMapping>

                <EntityTypeMapping TypeName="IsTypeOf(TestRelationshipsOnTPHModel.CustomerContact)">

                    <MappingFragment StoreEntitySet="CustomerContacts" >

                        <ScalarProperty Name="ContactId" ColumnName="ContactId" />

                        <Condition ColumnName="IsContactCustomer" Value="true" />

                    </MappingFragment>

                </EntityTypeMapping>

            </EntitySetMapping>

            <EntitySetMapping Name="Customers">

                <EntityTypeMapping TypeName="IsTypeOf(TestRelationshipsOnTPHModel.Customer)">

                    <MappingFragment StoreEntitySet="Customers">

                        <ScalarProperty Name="CompanyName" ColumnName="CompanyName" />

                        <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />

                    </MappingFragment>

                </EntityTypeMapping>

            </EntitySetMapping>

            <AssociationSetMapping Name="CustomerCustomerContact" TypeName="TestRelationshipsOnTPHModel.CustomerCustomerContact" StoreEntitySet="Contacts">

                <EndProperty Name="CustomerContact">

                    <ScalarProperty Name="ContactId" ColumnName="ContactId" />

                </EndProperty>

                <EndProperty Name="Customer">

                    <ScalarProperty Name="CustomerId" ColumnName="CustomerId" />

                </EndProperty>

                <Condition ColumnName="CustomerId" IsNull="false" />

            </AssociationSetMapping>

        </EntityContainerMapping>

    </Mapping>

</edmx:Mappings>

Thanks to Srikanth for his help.

Published Fri, Aug 29 2008 3:50 by Matthieu MEZIL

Leave a Comment

(required) 
(required) 
(optional)
(required)