【问题标题】:Configure entity with 2 properties of its own type using Fluent Api使用 Fluent Api 配置具有自身类型的 2 个属性的实体
【发布时间】:2018-08-13 09:23:07
【问题描述】:

我有一个名为 Process 的类,它有一个属性指向当前进程的父进程,另一个属性是当前进程的子进程列表。这两个属性又是 Process 类型。

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

     public int? ParentId { get; set; }
     public Process Parent { get; set; }

     public List<Process> Children { get; set; }

     // Nodes of the Process
     public List<WfModelItem> Nodes { get; set; }   // added 
}

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

    public int? ProcessId { get; set; }
    public Process Process  { get; set; }
}

当一个进程没有父进程时,它的ParentId是null,每个进程也可以有一些子进程。

我的配置如下:

public class ProcessConfiguration : EntityTypeConfiguration<Process>
{
    public ProcessConfiguration()
    {
        this.HasMany(c => c.Children)
            .WithMany();
        this.HasOptional(p => p.Parent)
            .WithMany();
    }
}

不幸的是,当我想通过 dbContext.Processes.Add(aProcess) 将其添加到 db 时,我收到此错误:

无法从“Process”类型的属性“Parent”与“Process”类型的属性“Parent”建立关系。检查 InversePropertyAttribute 中的值以确保关系定义是唯一的,并且从一个导航属性引用到其对应的反向导航属性

如何正确配置 Process 类?谢谢。

更新: 我在 Process 类中添加了另一个属性,以确保问题得到充分描述。

【问题讨论】:

    标签: c# entity-framework ef-fluent-api


    【解决方案1】:

    你可以试试这个吗:

    this
    .HasMany(oj => oj.Children)
    .WithOne(j => j.Parent)
    .HasForeignKey(j => j.ParentId );
    

    或者

    public class Process
    {
        public long ID { get; set; }
        public long? ParentID { get; set; }
    
        [InverseProperty("Children")]
        public Process Parent { get; set; }
        public virtual ICollection<Process> Children { get; set; }
    }
    

    【讨论】:

    • HasMany(u =&gt; u.Children) 不接受HasForeignKey(u =&gt; u.ID)
    • @ehsantoghian 你能用更新的答案重试吗?
    • @Thiery-v 我编辑了配置(fluent api 解决方案)。 HasMany() 不接受 WithOne()。我尝试了WithOptional()WithRequired()。但他们俩都创建了这个错误:Multiplicity constraint violated. The role 'WfModelItem_Process_Target' of the relationship 'Process' has multiplicity 1 or 0..1.
    【解决方案2】:

    经过两天的调查,我找到了解决方案。我将它发布给更多的读者。正如我在问题中解释的那样,每个 Process 可以有多个节点和子进程,因此一个节点可能属于一个进程及其子进程。当这种情况发生时,实体框架会抱怨'WfModelItem_Process_Source' 并引发以下异常:

    违反了多重性约束。关系“...”的角色“WfModelItem_Process_Source”具有多重性 1 或 0..1

    为了解决这个问题,我将 WfModelItem 类更改如下:

    public class WfModelItem 
    {
        public int Id { get; set; }
    
        public List<Process> Processes  { get; set; }
    }
    

    并更改了配置类:

    public class WfModelItemConfiguration : EntityTypeConfiguration<WfModelItem>
    {
        public WfModelItemConfiguration()
        {
            HasMany(n => n.Processes)
                .WithMany(p => p.Nodes);
        }
    }
    
    public class ProcessConfiguration : EntityTypeConfiguration<Process>
    {
        public ProcessConfiguration()
        {
            this.HasMany(oj => oj.Children)
                .WithOptional(j => j.Parent)
                .HasForeignKey(j => j.ParentId);
    
            this.HasMany(n => n.Nodes)
                .WithMany(p => p.Processes);
    
            this.HasOptional(p => p.Parent)
                .WithMany()
                .HasForeignKey(p => p.ParentId);
        }
    }
    

    希望它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多