zoukankan      html  css  js  c++  java
  • EntityFrameworkCore 一对一 && 一对多 && 多对多配置

    基本数据结构

    default
    表设计如下:
    入学记录

    public class AdmissionRecord
    {
        [Key]
        public long Id { get; set; }
        public DateTime AdmissionTime { get; set; }
        public string Remark { get; set; }
    }
    

    班级

    public class Class
    {
        [Key]
        public long Id { get; set; }
        public DateTime CreationTime { get; set; }
        public string ClassName { get; set; }
        public virtual List<Student> Students { get; set; }
    }
    

    学生-教师关系表

    public class StudentTeacherRelationship
    {
        [Key]
        public long Id { get; set; }
        public long StudentId { get; set; }
        public virtual Student Student { get; set; }
        public long TeacherId { get; set; }
        public virtual Teacher Teacher { get; set; }
    }
    

    教师

    public class Teacher
    {
        [Key]
        public long Id { get; set; }
        public string Name { get; set; }
        public string TeacherId { get; set; }
        public virtual List<StudentTeacherRelationship> StudentTeacherRelationships { get; set; }
    }
    

    学生

    public class Student
    {
        [Key]
        public long Id { get; set; }
        public string StudentId { get; set; }
        public string Name { get; set; }
        public long AdmissionRecordId { get; set; }
        //Student-AdmissionRecord 1:1
        [ForeignKey("AdmissionRecordId")]
        public virtual AdmissionRecord AdmissionRecord { get; set; }
        public long ClassId { get; set; }
        public virtual Class Class { get; set; }
        public virtual List<StudentTeacherRelationship> StudentTeacherRelationships { get; set; }
    }
    

    一对一

    学生-入学记录

    public long AdmissionRecordId { get; set; }
    //Student-AdmissionRecord 1:1
    [ForeignKey("AdmissionRecordId")]
    public virtual AdmissionRecord AdmissionRecord { get; set; }
    

    另解

    modelBuilder.Entity<Student>()
        .HasOne(p => p.AdmissionRecord)
        .WithOne(p => p.Student)
        .HasForeignKey<Student>(p => p.AdmissionRecordId);
    

    参考资料:
    Configuring One To One Relationships In Entity Framework Core

    一对多

    学生-班级

    modelBuilder.Entity<Class>()
        .HasMany(p => p.Students)
        .WithOne(p => p.Class)
        .HasForeignKey(p => p.ClassId)
        .OnDelete(DeleteBehavior.ClientSetNull);
    //下面写法也可以
    //modelBuilder.Entity<Student>()
    //    .HasOne(p => p.Class)
    //    .WithMany(p=>p.Students)
    //    .HasForeignKey(k => k.ClassId)
    //    .OnDelete(DeleteBehavior.ClientSetNull);
    

    多对多

    学生-教师

    //通过StudentTeacherRelationship中间表,通过实现两个1:n,实现m:n
    modelBuilder.Entity<StudentTeacherRelationship>()
        .HasOne(p => p.Student)
        .WithMany(p => p.StudentTeacherRelationships)
        .HasForeignKey(k => k.StudentId)
        .OnDelete(DeleteBehavior.ClientSetNull);
    
    modelBuilder.Entity<StudentTeacherRelationship>()
        .HasOne(p => p.Teacher)
        .WithMany(p => p.StudentTeacherRelationships)
        .HasForeignKey(k => k.TeacherId)
        .OnDelete(DeleteBehavior.ClientSetNull);
    

    示例代码

    示例代码

  • 相关阅读:
    [转]maven for eclipse在线安装 eclipsesr2
    js循环绑定事件解决方案
    设置 Eclipse/ 快速提示快捷键
    [转]POI 读取 Excel 转 HTML 支持 03xls 和 07xlsx 版本 包含样式
    解决子元素和父元素同时触发onclick
    【Tomcat】本地域名访问配置
    [ELK]快速搭建简单的日志分析平台
    Git 使用心得
    无光驱U盘启动WinPE安装操作系统的方法
    WMI调用发生 InitializationFailure 错误的解决过程
  • 原文地址:https://www.cnblogs.com/Lulus/p/9497874.html
Copyright © 2011-2022 走看看