简体   繁体   中英

EF Core - many to many with another foreign key

I need to create this tables:

Step (ID, Name)

Action (ID, Name)

StepActions(IdStep, IdAction, NextStep)

In Entity Framework:

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

I can create many-to-many relationship, but I don't know how to add relatioship for NextStep.

Thanks

Use these classes:


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

and this 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM