基础知识-关键字virtual
public class New
{
public int NewId { get; set; }
public string Title { get; set; }
public int NewTypeId { get; set; }
public virtual NewType NewType { get; set; }
}
public class NewType
{
public int NewTypeId { get; set; }
public string Name { get; set; }
public int BlogId { get; set; }
public virtual List<New> New { get; set; }
}
上面定义模型属性的时候有virtual关键字,表示延迟加载,我的理解是这样:当我访问主实体的时候,启动延迟加载,而不会查询数据库的子实体,只有要访问它的时候才会去数据库查询加载,泛型List表示此实体是一对多的关系。
EF默认是启动延迟加载的,我们如果不需要也可以手动禁止:
db.Configuration.LazyLoadingEnabled = false;

浙公网安备 33010602011771号