微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决自动插入第三个实体的导航属性

我正在使用 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 举报,一经查实,本站将立刻删除。