c# - 一对多和多对多关系实体框架,包括默认授权和角色管理
问题描述
我有一个生成一些在线课程的系统。在这个班上,我们有学生和老师。我已经Identity System and Authorization
为这个系统使用了微软的默认设置,但在我的设计中有些东西困扰着我。
有关更多解释,我想定义角色(教师、学生、管理员等),但是如何处理课程和教师之间的关系(它是一对多关系)和课程与学生之间的关系(它有多对多关系)非常令人困惑)。
所以我的问题是,在两个实体之间建立两种关系的真正方式是什么?如果不是,我应该如何处理?
这是我的课程实体
[Key]
[Display(Name = "شناسه")]
public Guid CourseId { get; set; }
[Required]
[Display(Name = "لوگوی دوره")]
public string LogoPath { get; set; }
[Required]
[Display(Name = "نام دوره")]
public string Name { get; set; }
[Required]
[Display(Name = "شرح دوره")]
public string Description { get; set; }
[Required]
[Display(Name = "شهریه")]
public int Price { get; set; }
[Display(Name = "دارای تخفیف")]
public bool HasDiscount { get; set; }
[Display(Name = "درصد تخفیف")]
public float DiscountPercentage { get; set; }
[Required]
[Display(Name = "آخرین تاریخ به روزرسانی")]
public DateTime LastUpdateUpdate { get; set; }
public string UserId { get; set; }
public AppUser CourseTeacher { get; set; }
public Guid CaptionId { get; set; }
public MainCaption CourseCaption{ get; set; }
public ICollection<Chapter> Chapters { get; set; }
public ICollection<AppUser> Students{ get; set; }
这是我的 AppUser 实体
[Required]
[Display(Name = "نام")]
public string Firstname { get; set; }
[Required]
[Display(Name = "نام خانوادگی")]
public string LastName { get; set; }
[Required]
[Display(Name = "جنسیت")]
public Gender Gender { get; set; }
[Display(Name = "عنوان")]
public string Title { get; set; }
[Display(Name = "اعتبار")]
public int Credit { get; set; }
[Display(Name = "تاریخ تولد")]
public string BirthDate { get; set; }
[Display(Name = "مدرک تحصیلی")]
public EducationalDegree? Degree { get; set; }
[Display(Name = "آدرس تصویر")]
public string ImagePath { get; set; }
[Display(Name = "تصویر تایید شده")]
public bool? IsImageConfirmed { get; set; }
[Display(Name = "آدرس فیس بوک")]
public string Facebook { get; set; }
[Display(Name = "آدرس اینستاگرام")]
public string Instagram { get; set; }
[Display(Name = "آدرس لینکداین")]
public string Linkedin { get; set; }
[Display(Name = "آدرس توئیتر")]
public string Twitter { get; set; }
[Display(Name = "آدرس وبسایت")]
public string Website { get; set; }
[Display(Name = "تاریخ ثبت نام")]
public DateTime RegisterDate { get; set; }
public ICollection<Course> StudentCourses { get; set; }
public ICollection<Course> TeacherCourses { get; set; }
public ICollection<News> WrittenNews { get; set; }
Tnx 到所有
编辑
我忘了说这包含一个错误Sequence contains more than one matching element
,这似乎合乎逻辑
一个重要的是,如果我使用相同的类进行继承,我应该如何为这两个表 AppUser 和 Course 添加两个关系
解决方案
我想定义角色(教师、学生、管理员等)
你可以通过几种不同的方式来做到这一点:
- 在应用程序级别拥有
User
和Role
表并强制执行角色,例如只有“教师”用户可以做教师的事情,只有学生可以注册课程等。 - 使用 EF,您可以使用继承。Abstract
User
将具有所有公共字段,而 Student、Teacher 和 Admin 将具有仅针对其角色的字段。
请看代码:
abstract class User
{
public int UserId { get; set; }
public string Name { get; set; }
}
class Teacher : User
{
public string Specialty { get; set; }
}
class Student : User
{
public int Grade { get; set; }
}
在此处查看更多信息- 本官方文档中给出的示例非常接近您想要实现的目标。
给学生的课程(它有多对多的关系)
对于这种类型的关系,我将使用复合 (StudentId, CourseId) 键创建一个新表/实体 StudentCourse。其原因是,通常您不仅希望在 2 个实体之间建立链接,而且还希望保留一些附加信息,例如Mark
,Performance
或EnrolmentDate
:
class StudentCourse
{
public int StudentId { get; set; }
public int CourseId { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
// Any additional fields related to the relationship
public int Mark { get; set; }
}
推荐阅读
- c - 打印出字符串中的第三个单词。C
- c# - 使用 EF Core 和 ExecuteSqlCommandAsync 从存储过程中的 SELECT 语句访问结果
- asp.net-core - 用于 SPA 和移动设备的 ASP.NET Core 2.1 Web-API 身份验证。从哪儿开始?
- ruby-on-rails - Devise on Rails 在我登录后让我登录并且不让我发帖
- performance - 并行计算机中应用程序的性能和可扩展性
- r - 如何从ggplot2的两列中绘制两个变量(相同的单位%)?
- java - 无法“findViewById”graphview
- node.js - 检测 observable 何时连续两次发出相同的值
- reactjs - 这个 Redux 代码中发生了什么......对 mapDispatchToProps、调度和连接感到困惑
- c++ - 我的 if 、 else if 和 else 循环有什么问题?