WCFDataService中的HierarchyID无法正常工作 [英] HierarchyID in WCFDataService not working

查看:73
本文介绍了WCFDataService中的HierarchyID无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

  我们正在为我们的应用程序使用WCF Dataservice。在此我们需要添加带有Hierarchy-id的表。当我将带有层次结构ID的表添加到EDMX文件时,它不会出现在类文件中。我该怎么做才能使用
的层次结构ID。我读到WCF Dataservice不支持层次结构ID,如果是这样,我怎样才能实现这个?

  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?

 

 

推荐答案

实体框架不支持SQL Server 2008中的hierarchyid数据类型,这意味着您无法在EF中使用它您的数据服务公开的模型。但是,您可以通过创建"自引用"来对层次结构进行建模。实体,您在其中定义
实体与其自身之间的关联。您可以使用
实体数据模型设计器
来完成此操作。如果您使用的是.NET Framework 4.0,我建议您还使用1:*关联创建外键属性,因为这样可以减少混淆。

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.

例如,您可以创建一个与一(1)个结尾的一对多(1:*)关联名为ParentNode,多个(*)结束名为ChildNodes。然后实体本身就有两个导航属性,一个名为ChildNodes,从
一个结束到多个结束,另一个名为ParentNode的导航属性
许多结束指向一个结尾。然后,这将映射到数据库中的单个表,其中ParentId(FK)和NodeId(PK)之间具有自引用关系。

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).

以下是此示例的示例.EDMX文件(在本例中我将使用整数而不是GUID):

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>


这篇关于WCFDataService中的HierarchyID无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆