List 使用Hashset初始化对象
我在实体框架的学习曲线中。我遵循msdn中实体框架中的教程。在样本中,实体定义如下:List 使用Hashset初始化对象,list,properties,entity,hashset,icollection,List,Properties,Entity,Hashset,Icollection,我在实体框架的学习曲线中。我遵循msdn中实体框架中的教程。在样本中,实体定义如下: public class Department { public Department() { this.Courses = new HashSet<Course>(); } // Primary key public int DepartmentID { get; set; } public string Name {
public class Department
{
public Department()
{
this.Courses = new HashSet<Course>();
}
// Primary key
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public System.DateTime StartDate { get; set; }
public int? Administrator { get; set; }
// Navigation property
public virtual ICollection<Course> Courses { get; private set; }
}
public class Course
{
public Course()
{
this.Instructors = new HashSet<Instructor>();
}
// Primary key
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
// Foreign key
public int DepartmentID { get; set; }
// Navigation properties
public virtual Department Department { get; set; }
public virtual ICollection<Instructor> Instructors { get; private set; }
}
public class Instructor
{
public Instructor()
{
this.Courses = new List<Course>();
}
// Primary key
public int InstructorID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public System.DateTime HireDate { get; set; }
// Navigation properties
public virtual ICollection<Course> Courses { get; private set; }
}
公共课部
{
公共部门()
{
this.Courses=newhashset();
}
//主键
public int DepartmentID{get;set;}
公共字符串名称{get;set;}
公共十进制预算{get;set;}
public System.DateTime开始日期{get;set;}
公共int?管理员{get;set;}
//导航属性
公共虚拟ICollection课程{get;private set;}
}
公共课
{
公共课程()
{
this.Instructors=new HashSet();
}
//主键
public int CourseID{get;set;}
公共字符串标题{get;set;}
公共整数积分{get;set;}
//外键
public int DepartmentID{get;set;}
//导航属性
公共虚拟部门部门{get;set;}
公共虚拟ICollection指令器{get;private set;}
}
公开课教师
{
公共讲师()
{
this.Courses=新列表();
}
//主键
公共int指令ID{get;set;}
公共字符串LastName{get;set;}
公共字符串名{get;set;}
public System.DateTime HireDate{get;set;}
//导航属性
公共虚拟ICollection课程{get;private set;}
}
我只展示了一些只关注我问题的课程。完整的样本是。我的怀疑是
为什么在构造函数中使用哈希集来创建ICollection属性?
hashset在set操作中的性能良好如果hashset适合这些类型的区域,那么
为什么它们在“讲师”实体的构造函数中使用列表初始化?
可能只是显示了2个选项。但如果没有,请给出建议,并告诉我使用hashset的其他一般情况。在这里,它们作为ICollection返回,只是在初始化时使用了hashset。我相信它们这样做是因为hashset强制唯一性。因此,一个系的课程必须是独一无二的。但对于一名教师来说,情况可能并非如此