简体   繁体   English

插入数据库多对多关系.net core 3.1

[英]insert into database many to many relationship .net core 3.1

I am trying to make a many to many relationship.我正在尝试建立多对多的关系。 I have made the following Classes for the tables.我为表格制作了以下课程。

public class Class {
  public int Id { get; set; }
  public string ClassName { get; set; }
  public string ClassRequirements { get; set; }
  public int HitDie { get; set; }
  public IList<ClassRace> ClassRaces { get; set; } 
}
public class Race {
  public int RaceID { get; set; }
  public string RaceName { get; set; }
  public string RaceFeatures { get; set; }
  public List<ClassRace> ClassRaces { get; set; }
}
public class ClassRace {
  public int ClassID { get; set; }
  public Class Class { get; set; }
  public int RaceID { get; set; }
  public Race Race { get; set; }
}

I initialize values in the tables when the program is ran for the first time.我在第一次运行程序时初始化表中的值。

var cl1 = new Class() {
  ClassName = "Barbarian",
  ClassRequirements = "13 Strength",
  HitDie = 12,
  ClassRaces = new List<ClassRace>() 
};
Race rc1 = new Race() {
  RaceName = "Elf",
  RaceFeatures = "DarkVision. +2 Wisdom",
  ClassRaces = new List<ClassRace>()
};
var cr = new ClassRace {
  Class = cl1,
  Race = rc1
};
rc0.ClassRaces.Add(cr);
cl1.ClassRaces.Add(cr);

On my DbContext I have this line of code which I believed will fill in my ClassID and RaceId automatically in the table however when I run the program this table will be empty and both rows are NULL .在我的DbContext ,我有这行代码,我相信它会自动在表中填写我的ClassIDRaceId但是当我运行程序时,这个表将是空的,并且两行都是NULL

public DbSet<Race> Races { get; set; }
public DbSet<Class> Classes { get; set; }
public DbSet<ClassRace> ClassRaces { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
  modelBuilder.Entity<ClassRace>().
    HasKey(cr => new { cr.ClassID, cr.RaceID });
  modelBuilder.Entity<ClassRace>().
    HasOne(x => x.Race).
    WithMany(x => x.ClassRaces).
    HasForeignKey(x => x.RaceID).
    OnDelete(DeleteBehavior.Restrict).
    IsRequired();
  modelBuilder.Entity<ClassRace>().
    HasOne(x => x.Class).
    WithMany(x => x.ClassRaces).
    HasForeignKey(x => x.ClassID).
    OnDelete(DeleteBehavior.Restrict).
    IsRequired();
}

Why is my ClassRaces table NULL .为什么我的ClassRaces表是NULL How do I fix this?我该如何解决?

I would recommend you just set the Id values as you are operating insert (especially focus on what you did in RaceClass model):我建议您在操作插入时设置 Id 值(特别关注您在 RaceClass 模型中所做的事情):

Will give you an example of data seeding in OnModelCreating method (You can also perform loops as i saw one of your comments):将为您提供 OnModelCreating 方法中的数据播种示例(您也可以执行循环,因为我看到了您的评论之一):

 modelBuilder.Entity<Class>().HasData(new Class
                {
                    Id = 1,
                    ClassName = "Barbarian",
                    ClassRequirements = "13 Strength",
                    HitDie = 12
                });



modelBuilder.Entity<Race>().HasData(new Race
                {
                     Id = 1,
                     RaceName = "Elf",
                     RaceFeatures = "DarkVision. +2 Wisdom"
                });

modelBuilder.Entity<ClassRace >().HasData(new ClassRace 
                {
                     Id = 1,
                     RaceId = 1,
                     ClassId= 1
                });

Call _context.SaveChanges() at bottom of you method.在方法底部调用 _context.SaveChanges()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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