FK到同一表代码的第一个实体框架 [英] FK to the Same Table Code First Entity Framework

查看:58
本文介绍了FK到同一表代码的第一个实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是实体框架中代码优先方法的新手.我对如何执行此操作感到有些困惑:

I am new to Code-First approach in Entity Framework. And I am a bit confused on how to do this:

我需要与同一张表建立FK关系,因此元素之间可以具有父级->子级关系.

I need a FK relationship to the same table, so I can have a Parent --> Child relationship between the elements.

这是表格的模型:

public class BucketGroup
{
   public int Id {get;set;} // This is the PK in the Table

   public string Name {get;set;}


   // Now this the FK, to this Same Table:
  public int? BucketGroupId {get;set;}

}

因此,我已将此项目设置为Nullable,如果 BucketGroupId 为NULL,那么我知道它是父项.

So I have made this item Nullable, if BucketGroupId is NULL then I know it is a parent Item.

我创建了一个测试项目并与Database First一起工作,而Model就是这样:

I created a test project and worked with Database First, and the Model is something like this:

public partial class Testing
{
    public Testing()
    {
        this.Testing1 = new HashSet<Testing>();
    }

    public int Id { get; set; }
    public Nullable<int> ParentId { get; set; }

    public virtual ICollection<Testing> Testing1 { get; set; }
    public virtual Testing Testing2 { get; set; }
}

因此,如果我向模型添加类似的属性,是否会使它成为 PK ID的 FK ?

So If I add a similar Property to my Model will that make it an FK to the PK Id?

public class BucketGroup
{
  public int Id {get;set;} // This is the PK in the Table

  public string Name {get;set;}


  // Now this the FK, to this Same Table:
  public int? BucketGroupId {get;set;}

  public virtual ICollection<BucketGroup> BucketGroup1 { get; set; }

}

这正确吗?

推荐答案

您有两个选择可以做到这一点:

You have two options to do that:

public class BucketGroup
{
  public int Id {get;set;} 

  public string Name {get;set;}

  [ForeignKey("ParentBucketGroup")]
  public int? ParentBucketGroupId {get;set;}

  public virtual BucketGroup ParentBucketGroup {get;set;}

  public virtual ICollection<BucketGroup> Children { get; set; }
}

或者,使用 Fluent Api :

public class BucketGroup
{
  public int Id {get;set;} 

  public string Name {get;set;}

  public int? ParentBucketGroupId {get;set;}

  public virtual BucketGroup ParentBucketGroup {get;set;}

  public virtual ICollection<BucketGroup> Children { get; set; }
}

并且,要配置关系,您可以在上下文中覆盖 OnModelCreating 方法:

And, to configure the relationship, you could override the OnModelCreating method on your context:

modelbuilder.Entity<BucketGroup>().HasOptional(b=>b.ParentBucketGroup )
                                  .WithMany(b=>b.Children )
                                  .HasForeignKey(b=>b.ParentBucketGroupId);

如果需要,可以使用单向(也称为单向)关系,但需要保留其中一种.

If you want, you can work with an one-directional (also called unidirectional) relationship, but you need to keep one of them.

如果删除 Children nav属性,则配置将如下所示:

If you remove the Children nav property, then, you configuration would be like this:

 modelbuilder.Entity<BucketGroup>().HasOptional(b=>b.ParentBucketGroup)
                                   .WithMany()
                                   .HasForeignKey(b=>b.ParentBucketGroupId);

或者,如果您删除 ParentBuketGroup 导航.属性,那么您需要执行以下操作:

Or, if you remove the ParentBuketGroup nav. property, then you need to do this:

 modelbuilder.Entity<BucketGroup>().HasOptional()
                                   .WithMany(b=>b.Children)
                                   .HasForeignKey(b=>b.ParentBucketGroupId);

这篇关于FK到同一表代码的第一个实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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