我实施了一项加载和保存酒店合同的网络服务。这是类结构(简化)
public class Contract
{
public int id { get; set; }
public virtual ICollection<Season> seasonList { get; set; }
public virtual ICollection<Room> roomList { get; set; }
}
public class Season
{
public int id { get; set; }
public Contract contract { get; set; }
public virtual ICollection<Season_Date> season_DateList { get; set; }
}
public class Room
{
public int id { get; set; }
public Contract contract { get; set; }
public virtual ICollection<Contingent> contingentList { get; set; }
}
public class Season_Date
{
public int id { get; set; }
public Season season { get; set; }
}
public class Contingent
{
public int id { get; set; }
public Season season { get; set; }
public Room room { get; set; }
}
对于加载方法,需要加载包含其所有详细信息的合同。我不想使用include,因为涉及到许多表和字段,我担心,由此产生的查询是复杂的。 有没有办法使用合同ID加载season_date和偶然记录?
答案 0 :(得分:0)
您可以使用延迟加载,这意味着第一次尝试访问它们时将从数据库加载相关实体(尽管这不适用于序列化)。您可以在Configuration
类的DbContext
属性上配置延迟加载:
public class MyDbContext : DbContext
{
public MyDbContext()
{
this.Configuration.LazyLoadingEnabled = true;
}
}
关系属性需要是虚拟的,但您已经这样做了。
如果您已经知道合同ID,那么为什么不能这样做呢?
var contract = myDbContext.Contracts.FirstOrDefault(c => c.id == idIWant);
有关加载相关实体的更多信息,请访问on this msdn page.