locked
HierarchyID in WCFDataService not working RRS feed

  • Question

  • Hi,

      We are using WCF Dataservice for our application. In this we need to add the table with Hierarchy-id. When i added the table with hierarchy ID to the EDMX file, its not appearing in the class file. What should i do to make use of Hierarchy ID. I read that WCF Dataservice is not supporting Hierarchy ID, If so how can i achieve this?

     

     

    Tuesday, November 30, 2010 2:21 PM

All replies

  • The Entity Framework doesn't support the hierarchyid data type in SQL Server 2008, which means that you can't use it in an EF model exposed by your data service. However, you can model hierarchies by creating a "self-referencing" entity where you define an association between the entity and itself. You can do this by using the Entity Data Model Designer. If you are using .NET Framework 4.0, I suggest also creating the foreign key property with a 1:* association because it makes it less confusing.

    For example, you can create a one-to-many (1:*) association with the one (1) end named ParentNode and the many (*) end named ChildNodes. Then the entity itself you would have two navigation properties, one named ChildNodes from the one end to the many end and a second navigation property called ParentNode on the many end pointing at the one end. This would then map to a single table in the database with a self-referencing relationship between ParentId (the FK) and NodeId (the PK).

    Here's an example of how this would look in the .EDMX file (I would use integers instead of the GUIDs in this example):

    <!-- From the SSDL -->
    <EntityType Name="TocNodes">
     <Key>
      <PropertyRef Name="NodeId" />
     </Key>
     <Property Name="NodeId" Type="uniqueidentifier" Nullable="false" />
     <Property Name="ParentId" Type="uniqueidentifier" Nullable="true" />
    </EntityType>
    <AssociationSet Name="TocNodeRecursive" Association="DocumentModel.Store.TocNodeRecursive">
     <End Role="ParentNode" EntitySet="TocNodes" />
     <End Role="ChildNodes" EntitySet="TocNodes" />
    </AssociationSet>
    <Association Name="TocNodeRecursive">
     <End Role="ParentNode" Type="DocumentModel.Store.TocNodes" Multiplicity="0..1" />
     <End Role="ChildNodes" Type="DocumentModel.Store.TocNodes" Multiplicity="*" />
     <ReferentialConstraint>
      <Principal Role="ParentNode">
       <PropertyRef Name="NodeId" />
      </Principal>
      <Dependent Role="ChildNodes">
       <PropertyRef Name="ParentId" />
      </Dependent>
     </ReferentialConstraint>
    </Association>
    <!-- From the CSDL -->
    <EntityType Name="TocNodes">
     <Key>
      <PropertyRef Name="NodeId" />
     </Key>
     <Property Type="Guid" Name="NodeId" Nullable="false" annotation:StoreGeneratedPattern="None" />
     <NavigationProperty Name="ChildNodes" Relationship="DocumentModel.TocNodeRecursive" FromRole="ParentNode" ToRole="ChildNodes" />
     <NavigationProperty Name="ParentNode" Relationship="DocumentModel.TocNodeRecursive" FromRole="ChildNodes" ToRole="ParentNode" />
     <Property Type="Guid" Name="ParentId" Nullable="true" />
    </EntityType>
    <AssociationSet Name="TopicsTocNode" Association="DocumentModel.TopicsTocNode">
     <End Role="Topics" EntitySet="Topics" />
     <End Role="TocNode" EntitySet="TocNodes" />
    </AssociationSet>
    <Association Name="TocNodeRecursive">
     <Documentation>
      <Summary>TOC Hierarchy</Summary>
      <LongDescription>Recursive relationship to track the TOC hierarchy.</LongDescription>
     </Documentation>
     <End Type="DocumentModel.TocNodes" Role="ParentNode" Multiplicity="0..1" />
     <End Type="DocumentModel.TocNodes" Role="ChildNodes" Multiplicity="*" />
     <ReferentialConstraint>
      <Principal Role="ParentNode">
       <PropertyRef Name="NodeId" />
      </Principal>
      <Dependent Role="ChildNodes">
       <PropertyRef Name="ParentId" />
      </Dependent>
    | </ReferentialConstraint>
    </Association>
    <!-- From the Mapping -->
    <EntitySetMapping Name="TocNodes">
     <EntityTypeMapping TypeName="IsTypeOf(DocumentModel.TocNodes)">
      <MappingFragment StoreEntitySet="TocNodes">
       <ScalarProperty Name="NodeId" ColumnName="NodeId" />
       <ScalarProperty Name="ParentId" ColumnName="ParentId" />
      </MappingFragment>
     </EntityTypeMapping>
    </EntitySetMapping>
    

     

     

     

    With this model, you should be able to access the data service like this: /service.svc/TocNodes/ChildNodes or /service.svc/TocNodes/ParentNode, to walk up and down the tree.

    Cheers,

    Glenn Gailey


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, December 3, 2010 8:20 AM