自动插入第三个实体的导航属性

问题描述

我正在使用 EntityFramework,代码优先,FLUENT API,System.Linq。

我有讲座,

讲座有一个规则组,规则组有很多学生。

但我还需要一个 StudentLecture 实体来跟踪特定学生的讲座出席情况。

所以Lecture也有很多学生,学生也有很多讲座。

我想要实现的是,当我插入到 Lecture 实体中时,定义了 RegulationsGroupId FK,我想将那个 RegulationsGruop 中的所有学生连接到那个讲座。

我可以将其配置为在 OnModelCreating 方法自动运行,还是必须重新查询讲座 ID,然后手动连接学生和讲座?

Lecture.cs

    using System;
    using System.Collections.Generic;
    
    namespace API.Entities
    {
        public class Lecture
        {
            public int LectureId { get; set; }
    
            public RegulationsGroup RegulationsGroup { get; set; }
    
            public int RegulationsGroupId { get; set; }
    
            public ICollection<StudentLecture> StudentLectures { get; set; }
        }
    }

AppUser.cs

using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;

namespace API.Entities
{
    public class AppUser : IdentityUser<int>
    {
        

        public RegulationsGroup RegulationsGruop { get; set; }


        public int? RegulationsGroupId { get; set; }


        //Only for students
        public ICollection<StudentLecture> StudentLectures { get; set; }

        
    }
}

学生讲座

namespace API.Entities
    {
        public class StudentLecture
        {
            public AppUser Student { get; set; }
    
            public int StudentId { get; set; }
    
            public Lecture Lecture { get; set; }
    
            public int LectureId { get; set; }
    
            public bool Attendance { get; set; }
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)