EF Core - 多对多与另一个外键

时间:2021-02-10 14:01:35

标签: c# entity-framework-core

我需要创建这个表:

步骤(ID,名称)

操作(ID、名称)

StepActions(IdStep, IdAction, NextStep)

在实体框架中:

public class Step
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<StepActions> StepActions { get; set; }
}

 public class Action
 {
        public int Id { get; set; }
        public virtual ICollection<StepActions> StepActions{ get; set; }
}

public class StepActions
{​​​​​​​
    public virtual Action Action {​​​​​​​ get; set; }​​​​​​​

    public virtual Step Step {​​​​​​​ get; set; }​​​​​​​

    public int Id {​​​​​​​ get; set; }​​​​​​​

    public int ActionID {​​​​​​​ get; set; }​​​​​​​

    public int StepID {​​​​​​​ get; set; }​​​​​​​
    public int NextStepID {​​​​​​​ get; set; }​​​​​​​
}​​​​​​​​​​​​​​

我可以创建多对多关系,但我不知道如何为 NextStep 添加关系。

谢谢

1 个答案:

答案 0 :(得分:1)

使用这些类:


public partial class Step
    {
        [Key]
        public int Id { get; set; }
       
        public string Name { get; set; }

        [InverseProperty(nameof(StepAction.NexStep))]
        public virtual ICollection<StepAction> StepActionNexSteps { get; set; }
        [InverseProperty(nameof(StepAction.Step))]
        public virtual ICollection<StepAction> StepActionSteps { get; set; }
    }


    public partial class Action
    {
        [Key]
        public int Id { get; set; }
       
        public string Name { get; set; }

        [InverseProperty(nameof(StepAction.Action))]
        public virtual ICollection<StepAction> StepActions { get; set; }
    }


 public partial class StepAction
    {
        [Key]
        public int Id { get; set; }
        public int StepId { get; set; }
        public int ActionId { get; set; }
        public int NexStepId { get; set; }

       [ForeignKey(nameof(StepId))]
        [InverseProperty("StepActionSteps")]
        public virtual Step Step { get; set; }
        [ForeignKey(nameof(ActionId))]
        [InverseProperty("StepActions")]
        public virtual Action Action { get; set; }
        [ForeignKey(nameof(NexStepId))]
        [InverseProperty("StepActionNexSteps")]
        public virtual Step NexStep { get; set; }
      
    }

还有这个 dbcontext:


public partial class StepsContext : DbContext
    {
        public StepsContext()
        {
        }

        public StepsContext(DbContextOptions<StepsContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Action> Actions { get; set; }
        public virtual DbSet<Step> Steps { get; set; }
        public virtual DbSet<StepAction> StepActions { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
          

            modelBuilder.Entity<StepAction>(entity =>
            {
                    entity.HasOne(d => d.Action)
                    .WithMany(p => p.StepActions)
                    .HasForeignKey(d => d.ActionId)
                    .OnDelete(DeleteBehavior.ClientSetNull);
                  
                entity.HasOne(d => d.NexStep)
                    .WithMany(p => p.StepActionNexSteps)
                    .HasForeignKey(d => d.NexStepId)
                    .OnDelete(DeleteBehavior.ClientSetNull);
                  
                entity.HasOne(d => d.Step)
                    .WithMany(p => p.StepActionSteps)
                    .HasForeignKey(d => d.StepId)
                    .OnDelete(DeleteBehavior.ClientSetNull);
           });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }