【问题标题】:FK to the Same Table Code First Entity FrameworkFK 到同表代码优先实体框架
【发布时间】:2015-06-13 12:15:52
【问题描述】:

我是实体框架中代码优先方法的新手。我对如何做到这一点有点困惑:

我需要与同一张表的 FK 关系,因此我可以在元素之间建立父 --> 子关系。

这是桌子的模型:

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 那么我知道它是一个父项目。

我创建了一个测试项目并使用 Database First,模型是这样的:

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; }
}

因此,如果我向我的模型添加一个类似的属性,这是否会使其成为 FKPK 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; }

}

这对吗?

【问题讨论】:

    标签: c# entity-framework ef-code-first


    【解决方案1】:

    您有两种选择:

    • 使用Data Annotations

      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 方法:

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

    更新

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

    如果您删除Children nav 属性,那么您的配置将是这样的:

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

    或者,如果您删除 ParentBuketGroup 导航。属性,那么你需要这样做:

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

    【讨论】:

    • 谢谢,我正在使用 FluentAPI,你能解释一下为什么我们必须同时添加:BucketGroup ParentBucketGroup AND ICollection Children
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2014-03-17
    • 2017-09-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多