问题描述
我有一个可以产生一些在线课程的系统。这节课我们有学生和老师。
我已为此系统使用Microsoft默认值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,您可以使用继承。摘要
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; }
}
查看更多信息here-该官方文档中提供的示例与您要实现的目标非常接近。
对学生的课程(有很多关系)
对于这种类型的关系,我将使用复合键(StudentId,CourseId)创建一个新的表/实体StudentCourse。这样做的原因是,通常您不只是想要两个实体之间的链接,还想要保留一些其他信息,例如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; }
}