How to split a data table?

In this msdn thread, zeeshan hirani wants to split a data table in two entities to do a sort of delay loading?

So how to do this?

I do this with Northwind:

<!-- EF Runtime content -->

<edmx:Runtime>

    <!-- SSDL content -->

    <edmx:StorageModels>

        <Schema Namespace="NorthwindModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">

            <EntityContainer Name="NorthwindModelStoreContainer">

                <EntitySet Name="Categories" EntityType="NorthwindModel.Store.Categories" store:Type="Tables" Schema="dbo" />

            </EntityContainer>

            <EntityType Name="Categories">

                <Key>

                    <PropertyRef Name="CategoryID" />

                </Key>

                <Property Name="CategoryID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />

                <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" />

                <Property Name="Description" Type="nvarchar" MaxLength="200" />

                <Property Name="Picture" Type="image" />

            </EntityType>

        </Schema>

    </edmx:StorageModels>

    <!-- CSDL content -->

    <edmx:ConceptualModels>

        <Schema Namespace="NorthwindModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">

            <EntityContainer Name="NorthwindEntities">

                <EntitySet Name="CategorySet" EntityType="NorthwindModel.Category" />

                <EntitySet Name="CategoryPictureSet" EntityType="NorthwindModel.CategoryPicture" />

                <AssociationSet Name="CategoryCategoryPicture" Association="NorthwindModel.CategoryCategoryPicture">

                    <End Role="Category" EntitySet="CategorySet" />

                    <End Role="CategoryPicture" EntitySet="CategoryPictureSet" />

                </AssociationSet>

            </EntityContainer>

            <EntityType Name="Category">

                <Key>

                    <PropertyRef Name="CategoryID" />

                </Key>

                <Property Name="CategoryID" Type="Int32" Nullable="false" />

                <Property Name="CategoryName" Type="String" Nullable="false" MaxLength="15" Unicode="true" FixedLength="false" />

                <Property Name="Description" Type="String" MaxLength="200" Unicode="true" FixedLength="false" />

                <NavigationProperty Name="Picture" Relationship="NorthwindModel.CategoryCategoryPicture" FromRole="Category" ToRole="CategoryPicture" a:SetterAccess="Private" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" />

            </EntityType>

            <EntityType Name="CategoryPicture">

                <Key>

                    <PropertyRef Name="CategoryID" />

                </Key>

                <Property Name="CategoryID" Type="Int32" Nullable="false" a:GetterAccess="Private" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" a:SetterAccess="Private" />

                <NavigationProperty Name="Category" Relationship="NorthwindModel.CategoryCategoryPicture" FromRole="CategoryPicture" ToRole="Category" />

                <Property Name="Picture" Type="Binary" Nullable="true" />

            </EntityType>

            <Association Name="CategoryCategoryPicture">

                <End Type="NorthwindModel.Category" Role="Category" Multiplicity="1" />

                <End Type="NorthwindModel.CategoryPicture" Role="CategoryPicture" Multiplicity="1" />

                <ReferentialConstraint>

                    <Principal Role="Category">

                        <PropertyRef Name="CategoryID" />

                    </Principal>

                    <Dependent Role="CategoryPicture">

                        <PropertyRef Name="CategoryID" />

                    </Dependent>

                </ReferentialConstraint>

            </Association>

        </Schema>

    </edmx:ConceptualModels>

    <!-- C-S mapping content -->

    <edmx:Mappings>

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

            <EntityContainerMapping StorageEntityContainer="NorthwindModelStoreContainer" CdmEntityContainer="NorthwindEntities">

                <EntitySetMapping Name="CategorySet">

                    <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.Category)">

                        <MappingFragment StoreEntitySet="Categories">

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

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

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

                        </MappingFragment>

                    </EntityTypeMapping>

                </EntitySetMapping>

                <AssociationSetMapping Name="CategoryCategoryPicture" TypeName="NorthwindModel.CategoryCategoryPicture" StoreEntitySet="Categories">

                    <EndProperty Name="CategoryPicture">

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

                    </EndProperty>

                    <EndProperty Name="Category">

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

                    </EndProperty>

                </AssociationSetMapping>

                <EntitySetMapping Name="CategoryPictureSet">

                    <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.CategoryPicture)">

                        <MappingFragment StoreEntitySet="Categories">

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

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

                        </MappingFragment>

                    </EntityTypeMapping>

                </EntitySetMapping>

            </EntityContainerMapping>

        </Mapping>

    </edmx:Mappings>

</edmx:Runtime>

 

Then, I extend (partial) the CategoryPicture class to do a constructor with a Category parameter. Indeed, we can’t create a CategoryPicture without a Category because CategoryName doesn’t allow null.

partial class CategoryPicture

{

    public CategoryPicture(Category category)

    {

        Category = category;

        CategoryID = category.CategoryID;

    }

}

Published Thu, Nov 13 2008 15:06 by Matthieu MEZIL

Comments

# How to split a data table v2?

I wrote a post which explains how to split a data table . But there is a bug in EF and so I couldn't

Tuesday, November 25, 2008 11:07 AM by Matthieu MEZIL

Leave a Comment

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