Entity Framework using语句延迟加载
Entity Framework lazy loading in using statement
我很好奇,当我深入研究 Entity Framework 时,我很乐意帮助我理解我在这里展示的这个例子中的延迟加载:
public partial class Customer
{
public int CustomerID { get; set; }
public Nullable<int> PersonID { get; set; }
public Nullable<int> StoreID { get; set; }
public Nullable<int> TerritoryID { get; set; }
public string AccountNumber { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual Territory Territory { get; set; }
}
public partial class Territory
{
public Territory()
{
this.Customers = new HashSet<Customer>();
}
public int TerritoryID { get; set; }
public string Name { get; set; }
public string CountryRegionCode { get; set; }
public string C_Group_ { get; set; }
public decimal SalesYTD { get; set; }
public decimal SalesLastYear { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public ActionResult LoadData()
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
var pageSize = length != null ? Convert.ToInt32(length) : 0;
var skip = start != null ? Convert.ToInt32(start) : 0;
int totalRecords = 0;
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
//1.
var items = dc.Customers.Select(a => a);
//2.
//var items = dc.Customers.Select(a => new
//{
// a.CustomerID,
// a.PersonID,
// a.StoreID,
// TerritoryName = a.Territory.Name,
// a.AccountNumber,
// a.ModifiedDate
//});
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
items = items.OrderBy(sortColumn + " " + sortColumnDir);
}
totalRecords = items.Count();
var data = items.Skip(skip).Take(pageSize).ToList();
return Json(new { recordsFiltered = totalRecords, recordsToral = totalRecords, data = data }, JsonRequestBehavior.AllowGet);
}
}
我已经对一些部分进行了编号。在第一种情况下,如您所见,我 select 所有客户,延迟加载是正确的,这会引发错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
如果我取消注释第二部分,它将正常工作。它们之间的唯一区别是,在第二种情况下,我需要 select 列
这是因为JsonResult
(来自return Json(...)
语句)将在using
块离开后由ASP执行,它会尝试序列化所有字段你的 Customer
对象,并会在你的 dbcontext 被处理后尝试访问 Territory
引用 属性。
我很好奇,当我深入研究 Entity Framework 时,我很乐意帮助我理解我在这里展示的这个例子中的延迟加载:
public partial class Customer
{
public int CustomerID { get; set; }
public Nullable<int> PersonID { get; set; }
public Nullable<int> StoreID { get; set; }
public Nullable<int> TerritoryID { get; set; }
public string AccountNumber { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual Territory Territory { get; set; }
}
public partial class Territory
{
public Territory()
{
this.Customers = new HashSet<Customer>();
}
public int TerritoryID { get; set; }
public string Name { get; set; }
public string CountryRegionCode { get; set; }
public string C_Group_ { get; set; }
public decimal SalesYTD { get; set; }
public decimal SalesLastYear { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public ActionResult LoadData()
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
var pageSize = length != null ? Convert.ToInt32(length) : 0;
var skip = start != null ? Convert.ToInt32(start) : 0;
int totalRecords = 0;
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
//1.
var items = dc.Customers.Select(a => a);
//2.
//var items = dc.Customers.Select(a => new
//{
// a.CustomerID,
// a.PersonID,
// a.StoreID,
// TerritoryName = a.Territory.Name,
// a.AccountNumber,
// a.ModifiedDate
//});
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
items = items.OrderBy(sortColumn + " " + sortColumnDir);
}
totalRecords = items.Count();
var data = items.Skip(skip).Take(pageSize).ToList();
return Json(new { recordsFiltered = totalRecords, recordsToral = totalRecords, data = data }, JsonRequestBehavior.AllowGet);
}
}
我已经对一些部分进行了编号。在第一种情况下,如您所见,我 select 所有客户,延迟加载是正确的,这会引发错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
如果我取消注释第二部分,它将正常工作。它们之间的唯一区别是,在第二种情况下,我需要 select 列
这是因为JsonResult
(来自return Json(...)
语句)将在using
块离开后由ASP执行,它会尝试序列化所有字段你的 Customer
对象,并会在你的 dbcontext 被处理后尝试访问 Territory
引用 属性。