C# 实体框架:具有不同ParrentID的自引用树

C# 实体框架:具有不同ParrentID的自引用树,c#,entity-framework-4,C#,Entity Framework 4,我有这样的课: class Tree { [Key] public int TreeID { get; set; } public int? ParentID { get; set; } public int ID { get; set; } public string Name { get; set; } public virtual Tree Parrent { get; set; } public virtua

我有这样的课:

  class Tree
  {
     [Key]
     public int TreeID { get; set; }
     public int? ParentID { get; set; }
     public int ID { get; set; }
     public string Name { get; set; }

     public virtual Tree Parrent { get; set; }
     public virtual ICollection<Tree> Children { get; set; }
 }
类树
{
[关键]
公共int树ID{get;set;}
public int?ParentID{get;set;}
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟树Parrent{get;set;}
公共虚拟ICollection子项{get;set;}
}
和配置类:

class TreeConfiguration : EntityTypeConfiguration<Tree>
{
    public TreeConfiguration()
    {
        this.HasOptional(d => d.Parrent)
            .WithMany(p => p.Children)
            .HasForeignKey( d => d.ParentID)
            .WillCascadeOnDelete(false);
    }

}
类树配置:EntityTypeConfiguration
{
公共树配置()
{
this.has可选(d=>d.Parrent)
.有许多(p=>p.儿童)
.HasForeignKey(d=>d.ParentID)
.WillCascadeOnDelete(假);
}
}
它工作得很好,但我想要的是子节点使用parrent节点的ID作为ParentID,而不是TreeID

它应该是这样工作的: 每个节点都有一个id(即id)和其父节点的id(即ParentID)。TreeID是主键,它与子-父映射无关-它仅用于数据库


我不能更改列,所以我必须这样做。。显然,这与FKs必须始终指向主键这一事实有关。所以这是不可能的。

为什么不使用您的“ID”作为唯一列-例如,为它使用唯一索引? 那么你就有了与ID为PK时相同的语义