简体   繁体   English

实体框架dbset有哪些模型?

[英]entity framework dbset which models?

Using entity framework 6 and mvc 5, assuming I have the following models: 使用实体框架6和mvc 5,假设我具有以下模型:

class Employee 
{
   public int EmployeeID { get; set; }
   public String Name { get; set; }
   public Department Department { get; set; }
}

class Department 
{
   public int DepartmentID { get; set; }
   public String DepartmentName { get; set; }
   public int FloorNumber { get; set; }
}

In my DbContext class where the DbSet goes, do I do DbSet<Employee> only or do I also have to do DbSet<Department> ? 在我DbContext类,其中DbSet去,我该怎么办DbSet<Employee>仅或者我也要做DbSet<Department> Right now I'm only doing DbSet<Employee> because the way I understand it, the main model is Employee which has a complex type Department inside, so theoretically if Employee loads then Department should load passively (load as in create a table)? 现在我只在做DbSet<Employee>因为据我了解,它的主要模型是Employee ,它内部有一个复杂的Department类型,因此理论上,如果Employee加载,那么Department应该被动加载(就像在创建表中那样加载)?

Currently, I have Department as a virtual field for lazy loading because when I try to access it without the virtual attribute I get a null pointer exception . 当前,我将Department作为virtual字段进行延迟加载,因为当我尝试在没有virtual属性的情况下访问它时,我会得到一个null pointer exception

Also, my model is a little more complex than this with multiple complex objects, do each of these need their own DbSet ? 另外,我的模型比具有多个复杂对象的模型要复杂一些,每个模型都需要自己的DbSet吗?

Thank you 谢谢

According to the documentation (emphasis is mine): 根据文档 (重点是我的):

Type Discovery 类型发现

<snip> <剪断>

In the following example, there is only one DbSet property defined on the SchoolEntities class (Departments). 在下面的示例中,在SchoolEntities类(部门)上仅定义了一个DbSet属性。 Code First uses this property to discover and pull in any referenced types. Code First使用此属性来发现并提取任何引用的类型。

However, if you ever wish to manipulate lists of Employee s independent from your Department , then you should include it. 但是,如果您希望独立于Department来操纵Employee的列表,则应将其包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM