c# – 实体框架的Find方法如何工作?
我正在学习实体框架,并且面对一些我无法理解的Find()方法.
摘自Julia Lerman的书“编程实体框架:代码优先” public class Destination { public int DestinationId { get; set; } public string Name { get; set; } public string Country { get; set; } public string Description { get; set; } public byte?[] Photo { get; set; } public ICollection<Lodging> Lodgings { get; set; } public Destination() { Lodgings = new List<Lodging>(); } } public class Lodging { public int LodgingId { get; set; } public string Name { get; set; } public string Owner { get; set; } public bool IsResort { get; set; } public Destination Destination { get; set; } } 我通过以下方式处理数据: var destination = organizationDbContext.Destinations // case # 1 .Include("Lodgings") .First(p=>p.DestinationId==1); var destination = organizationDbContext.Destinations.Find(1); // case # 2 >为什么我不能在Include()调用后的第一种情况下调用Find()方法,但可以使用Where()和First()? 我的问题可以用另一种方式表达: >正确的做法是:找到一个对象并加载所有相关的内部对象(一对多)? 解决方法
重点是Find通过在上下文的本地缓存中搜索开始.如果未找到匹配项,则会向db发送查询.
我认为这是内在的解释,没有Find on IQueryable. var destination = organizationDbContext.Destinations // case # 1 .Include("Lodgings") .First(p=>p.DestinationId==1); 更多信息:https://msdn.microsoft.com/en-us/data/jj573936.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |