Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用实体框架映射层次结构中的父子关系_Entity Framework_Entity Framework 5 - Fatal编程技术网

Entity framework 使用实体框架映射层次结构中的父子关系

Entity framework 使用实体框架映射层次结构中的父子关系,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,我们使用的是实体框架,我们有一个独特的需求,在尝试了许多可能的选项后,我不知道如何做到这一点,下面是问题总结 我有以下实体 public class SuperParent { [Key] public int SupoerParentId {get;set;} public virtual ICollection<Parent> Intermediates {get;set;} } public class Parent { [Key]

我们使用的是实体框架,我们有一个独特的需求,在尝试了许多可能的选项后,我不知道如何做到这一点,下面是问题总结

我有以下实体

public class SuperParent
{
     [Key]
     public int SupoerParentId {get;set;}
     public virtual ICollection<Parent> Intermediates {get;set;}
}

public class Parent
{
     [Key]
     public int ParentId {get;set;}
     [ForeignKey("SuperParentId")]
     public virtual SuperParent Ancestor {get;set;}
     public int SuperParentId {get;set;}
     public virtual ICollection<Child> Children {get;set;}
}

public class Child
{
     [Key]
     public int ChildId {get;set;}
     [ForeignKey("ParentId")]
     public virtual Parent Ancestor {get;set;}
     public int ParentId {get;set;}

     /// Area of guidance required here..............
     /// I just want to some what denormalize table and add SuperParentId also in
     /// Child and in Database. As most of time its child we query and its very
     /// efficient for us to directly query based on SuperParentId, I want to do 
     /// something like below:
     [ForeignKey("SuperParentId")]
     public virtual SuperParent Ancestor {get;set;}
     public int SuperParentId SuperAncestorId {get;set;}
}
公共类超级家长
{
[关键]
公共int SupoerParentId{get;set;}
公共虚拟ICollection中间产物{get;set;}
}
公共类父类
{
[关键]
public int ParentId{get;set;}
[外键(“SuperParentId”)]
公共虚拟超级parent祖先{get;set;}
public int SuperParentId{get;set;}
公共虚拟ICollection子项{get;set;}
}
公营儿童
{
[关键]
public int ChildId{get;set;}
[ForeignKey(“ParentId”)]
公共虚拟父祖先{get;set;}
public int ParentId{get;set;}
///此处所需的指导范围。。。。。。。。。。。。。。
///我只想对表进行一些非规范化,并在其中添加SuperParentId
///在数据库中的子级和。大多数时候,我们查询它的子级和它的
///高效的让我们直接查询基于SuperParentId的,我想做的
///如下所示:
[外键(“SuperParentId”)]
公共虚拟超级parent祖先{get;set;}
public int SuperParentId supercancestorid{get;set;}
}
我们有1:N:N的关系,很多时候我们只是想绕过父母,从超级父母到直接想接触孩子。。。目前多级联接存在问题,我们的查询效率不高,我们存储了大量数据,每个表有20多列

问题:

  • EF有可能吗?那我怎么写modelBinder呢 在模型创建中支持这一点
  • 还有其他选择吗

  • 如果您想要这种设计,那么默认情况下,子级将从父级和超级父级进行级联删除,即

    如果依赖实体上的外键不可为空,则代码 首先在关系上设置级联删除

    您只需从
    SuperParent
    中删除默认的级联删除即可

    modelBuilder.Entity<Child>()
       .HasRequired(c => c.Ancestor)
       .WithMany()
       .WillCascadeOnDelete(false);
    
    使用多个
    更改上面的

        .WithMany(sp => sp.Children)
    
    public DbSet<Child> Children { get; set; }
    
        .WithMany(sp => sp.Children)