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>


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

        FROM Contacts



<EntityType Name=CustomerContacts>


        <PropertyRef Name=ContactId />


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

    <Property Name=IsContactCustomer Type=bit />


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


    <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 />



                <EntityTypeMapping TypeName=IsTypeOf(TestRelationshipsOnTPHModel.CustomerContact)>

                    <MappingFragment StoreEntitySet=CustomerContacts >

                        <ScalarProperty Name=ContactId ColumnName=ContactId />

                        <Condition ColumnName=IsContactCustomer Value=true />




            <EntitySetMapping Name=Customers>

                <EntityTypeMapping TypeName=IsTypeOf(TestRelationshipsOnTPHModel.Customer)>

                    <MappingFragment StoreEntitySet=Customers>

                        <ScalarProperty Name=CompanyName ColumnName=CompanyName />

                        <ScalarProperty Name=CustomerId ColumnName=CustomerId />




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

                <EndProperty Name=CustomerContact>

                    <ScalarProperty Name=ContactId ColumnName=ContactId />


                <EndProperty Name=Customer>

                    <ScalarProperty Name=CustomerId ColumnName=CustomerId />


                <Condition ColumnName=CustomerId IsNull=false />





Thanks to Srikanth for his help.

This entry was posted in 7671, 7674, 7675. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>