实体框架4急切加载不起作用! [英] Entity Framework 4 eager loading not working at all!

查看:153
本文介绍了实体框架4急切加载不起作用!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将linq2sql转换为实体框架。



在转换期间,我需要使用包含用于eagar加载的linq2sql转换负载,但是急切加载不起作用。当我使用分析器时,我发现子实体被加载,它们被访问。

  DataBaseEntities context = new V3C_DataBaseEntities() ; 

context.Agents.Include(Account);

代理ag = context.Agents.Where(x => x.Login_ID ==2)。SingleOrDefault();

//这里的帐号应该已经被加载,
//但是实际上它们被加载了下面的行被执行。

Console.WriteLine(ag.Account.ID.ToString());

如果执行以下操作,它工作得很好,但我必须按照问题的方式进行说明。 / p>

  var c =(from ag in context.Agents.Include(Account)
其中ag.Login_ID == 2
select ag).SingleOrDefault();

我也想要一种加载子实体的类型安全方式。

解决方案

你不能这样写,你写的有问题。您必须在查询中使用include。从Linq2Sql转换为EF时,没有平滑的方式。您必须接受新的API并正确使用它。



您只能使用以下内容:

  var query = context。 Agents.Include( 账户); 
代理ag = query.Where(x => x.Login_ID ==2)。SingleOrDefault();

如果您要输入安全的包含版本,您必须下载实体框架功能CTP5 或写下您的自己的扩展名


I am converting linq2sql to entity framework.

During the conversion I needed to convert the loadwith of linq2sql with include for eagar loading, but the eager loading is not working. When I used the profiler, I found that the child entities are loaded with they are accessed.

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());

If do the following, It works perfectly, but I must do the way mentioned in the question.

var c = (from ag in context.Agents.Include("Account")
                     where ag.Login_ID == "2"
                     select ag).SingleOrDefault();

I would also like a type safe way of loading child entities.

解决方案

You can't do that as you wrote in question. You must use include in query. There is no smooth manner when transforming from Linq2Sql to EF. You must accept new API and use it correctly.

You can use only this:

var query = context.Agents.Include("Account");
Agent ag = query.Where(x => x.Login_ID == "2").SingleOrDefault(); 

If you want type safe Include version you must download Entity Framework Feature CTP5 or write your own extension.

这篇关于实体框架4急切加载不起作用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆