【问题标题】:EF Core - many to many with another foreign keyEF Core - 多对多与另一个外键
【发布时间】:2021-02-10 14:01:35
【问题描述】:

我需要创建这个表:

步骤(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 添加关系。

谢谢

【问题讨论】:

    标签: c# entity-framework .net-core entity-framework-core


    【解决方案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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2021-01-14
      • 2018-11-08
      相关资源
      最近更新 更多