【发布时间】:2015-01-09 09:04:06
【问题描述】:
我处于实体框架的学习曲线中。我按照msdn中实体框架中的教程进行操作。在示例中,实体定义如下;
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; }
}
我只展示了一些只关注我的问题的课程。完整样本为here。我的疑问是 为什么在构造函数中使用 hashset 来创建 ICollection 属性?
hashset 在集合操作中表现良好如果hashset在这些类型的领域是合适的,那么
为什么他们在 'Instructor' 实体的构造函数中使用 List 进行初始化?
可能只是显示 2 个选项。但如果不是,请建议并告诉我使用 hashset 的任何其他一般场景。在这里,它们以 ICollection 的形式返回,仅在初始化时使用了 hashset。
【问题讨论】:
标签: list properties entity hashset icollection