EF建立自我关系 [英] EF Build self relationship

查看:76
本文介绍了EF建立自我关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一次我有一个简单的问题:

this time i have a simple question:

我有一张与自己相关的桌子

I have one table, related with himself

=====================
| Id                |  
| IdParent          |
| NodeName          | 
=====================

IdParent 可以有0个或多个 Id
问题是,如何使用FluentApi或数据注释配置这种关系。

Where IdParent Could Have 0 or many Id. The question is, how can i configure this relationship using FluentApi or data anotations.

这种关系的结果是像这样的一棵树(具有两个级别) :

The result of this relationship is a tree (with two levels) like this:

a
|---b
|---c 
d
e
f---g    

其中(a,d,e,f)是父节点,(b,c,g)是子节点。

where (a,d,e,f) are parent nodes and (b,c,g) are child nodes.

非常感谢Tks。

推荐答案

实体框架支持自引用外键。目前尚不清楚您是数据库第一人还是代码第一人。

Entity framework supports self-referencing foreign keys. It's not clear if you are a database first, or codefirst person.

如果您是数据库第一人,只需在数据库中创建自引用外键,即可从数据库中更新模型。至少从EF 4开始,EF就一直支持这种样式的键。

If you're a database first person, just create a self-referencing foreign key in your database, the update the model from the DB. EF has supported this style of key since at least EF 4.

从代码优先的角度来看,这可能会有些棘手。如果您使用的是EF 4.x,则需要覆盖 OnModelCreating 函数(请参见此处的示例)。如果您使用的是EF 5(来自.net 4.5 Beta或2011年6月的CTP),则提供了一些很好的指导。简短的做法是在模型上使用 [ForeignKey( IdParent)] 属性。

From a code first perspective, it may a bit trickier. If you're using EF 4.x you need to override the OnModelCreating function (see example here). If you're using EF 5 (from the .net 4.5 beta, or the June 2011 CTP), then this question provides some good guidance. The short of it is to use [ForeignKey("IdParent")] attribute on your model.

您应该会看到类似...的XML。

Either way you should see XML something like ...

  <edmx:Runtime>
    <edmx:StorageModels>
    <Schema Namespace="...">
          <AssociationSet Name="FK_Culture_has_ParentCulture" 
                   Association="AL_Model.Store.FK_Culture_has_ParentCulture">
            <End Role="Culture" EntitySet="Culture" />
            <End Role="Culture1" EntitySet="Culture" />
          </AssociationSet>

 .....
 .....
 <edmx:ConceptualModels>
    <Schema Namespace="...">
    <EntityContainer Name="..." >
      <Association Name="FK_Culture_has_ParentCulture">
        <End Role="Culture" Type="AL_Model.Store.Culture" Multiplicity="0..1" />
        <End Role="Culture1" Type="AL_Model.Store.Culture" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Culture">
              <PropertyRef Name="CultureID" />
            </Principal>
            <Dependent Role="Culture1">
              <PropertyRef Name="ParentCultureID" />
            </Dependent>
          </ReferentialConstraint>
      </Association>

自引用结构

此语句引起了一点注意:

This statement warrents a little attention:

Where IdParent Could Have 0 or many Id. 

要具有零或parentID,您将需要表中包含零作为ID的条目。为什么?因为外键需要链接到某物。但是,我怀疑您想要的是使IdParent可为空。您的问题未指定数据库,但是大多数主要数据库仅在有值时才强制使用外键。换句话说,IdParent中的Null表示没有父母。

In order to have "zero, or a parentID" then you're going to need to have an entry in your table containing zero as an ID. Why? because foreign keys need to link to something. What I suspect you want, however, is to make IdParent nullable. Your question doesn't specify database, but most major databases enforce foreign keys only when there is a value. Said another way, Null in IdParent means there is no parent.

这篇关于EF建立自我关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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