编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Entity Framework Core-使用Fluent API配置多对多关系

wxchong 2024-07-11 01:08:32 开源技术 9 ℃ 0 评论

通过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}
接下来在DbContext的OnModelCreating方法内使用UsingEntity方法来创建实体之间关联,在下面代码中我们给与TeacherStudent作为关联实体
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")); }}
当我们执行migrations,EF Core 将会创建一个名字为TeacherStudent的关联表,这张表将包含Teacher和Student的外键,如下图所示:


Student & Teacher 这两个实体是多对多的关系,意味着一个学生有多个老师,同时一个老师又有多个学生

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}
第三步 – 在DBContext的OnModelCreating()方法中做配置
modelBuilder.Entity<TeacherStudent>().HasKey(t => new { t.StudentId, t.TeacherId });
第四步– 使用Fluent API创建一对多关系
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); }}

总结

这节我们主要介绍了在EF Core中使用Fluent API配置多对多关系
源代码地址
https://github.com/bingbing-gui/Asp.Net-Core-Skill/tree/master/EntityFrameworkCore/EFCoreFluentAPIManyToMany

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表