【发布时间】:2011-03-03 22:31:45
【问题描述】:
我正在将 linq2sql 转换为实体框架。
在转换过程中,我需要将 linq2sql 的 loadwith 与 include 转换为 eagar 加载,但急切加载不起作用。当我使用分析器时,我发现子实体加载了它们被访问。
DataBaseEntities context = new V3C_DataBaseEntities();
context.Agents.Include("Account");
Agent ag = context.Agents.Where(x => x.Login_ID == "2").SingleOrDefault();
// here the account should have been loaded,
// but actually they are loaded with the line below this is executed.
Console.WriteLine(ag.Account.ID.ToString());
如果执行以下操作,它可以完美运行,但我必须按照问题中提到的方式进行。
var c = (from ag in context.Agents.Include("Account")
where ag.Login_ID == "2"
select ag).SingleOrDefault();
我还想要一种类型安全的加载子实体的方法。
【问题讨论】: