通过Fluent API 配置EF Core多对多关系,首先我们需要在两个类中添加集合导航属性,接着使用UsingEntity方法添加关联表
1 例子:Fluent API多对多关系
如下两个实体类 Student & Teacher
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Teacher> Teacher { get; set; } //collection navigation property
}
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Student> Student { get; set; } //collection navigation property
}
public class SchoolContext : DbContext
{
public DbSet<Teacher> Teacher { get; set; }
public DbSet<Student> Student { get; set; }
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Write Fluent API configurations here
modelBuilder.Entity<Teacher>()
.HasMany(t => t.Student)
.WithMany(t => t.Teacher)
.UsingEntity(j => j.ToTable("TeacherStudent"));
}
}
2 EF Core 5.0 以及之前版本
针对EF Core 5.0以及之前的版本,创建多对多关系的过程是不同的,我们首先需要创建一个关联实体,我们将这个关联名称叫做TeacherStudent,你也可以起个别的名字
关联表包含了两个实体的外键,该实体的主键由这两个外键组合而成
public class TeacherStudent
{
public Student Student { get; set; }
public Teacher Teacher { get; set; }
}
通过下列配置多对多关系
第一步 – 在关联表中添加外键属性
public class TeacherStudent
{
public int StudentId { get; set; } //foreign key property
public Student Student { get; set; } //Reference navigation property
public int TeacherId { get; set; } //foreign key property
public Teacher Teacher { get; set; } //Reference navigation property
}
第一步 – 在另外实体中添加集合导航属性
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public IList<TeacherStudent> TeacherStudent { get; set; } //collection navigation property
}
public class Teacher
{
public int Id { get; set; }
public string Name { get; set; }
public IList<TeacherStudent> TeacherStudent { get; set; } //collection navigation property
}
modelBuilder.Entity<TeacherStudent>().HasKey(t => new { t.StudentId, t.TeacherId });
modelBuilder.Entity<TeacherStudent>()
.HasOne(t => t.Student)
.WithMany(t => t.TeacherStudent)
.HasForeignKey(t => t.StudentId);
modelBuilder.Entity<TeacherStudent>()
.HasOne(t => t.Teacher)
.WithMany(t => t.TeacherStudent)
.HasForeignKey(t => t.TeacherId);
全部代码
public class SchoolContext : DbContext
{
public DbSet<Teacher> Teacher { get; set; }
public DbSet<Student> Student { get; set; }
public DbSet<TeacherStudent> TeacherStudent { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server=vaio;Database=Company;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Write Fluent API configurations here
modelBuilder.Entity<TeacherStudent>().HasKey(t => new { t.StudentId, t.TeacherId });
modelBuilder.Entity<TeacherStudent>()
.HasOne(t => t.Student)
.WithMany(t => t.TeacherStudent)
.HasForeignKey(t => t.StudentId);
modelBuilder.Entity<TeacherStudent>()
.HasOne(t => t.Teacher)
.WithMany(t => t.TeacherStudent)
.HasForeignKey(t => t.TeacherId);
}
}
总结
本文暂时没有评论,来添加一个吧(●'◡'●)