简体   繁体   English

在Linqpad中获取实体框架上下文?

[英]Getting entity framework context in Linqpad?

I've created a DLL assembly which contains the edmx for northwind database. 我创建了一个DLL程序集,其中包含用于northwind数据库的edmx

I created a reference to that dll through linqpad and I do see the db and able to run queries. 我通过linqpad创建了对该DLL的引用,我确实看到了db并且能够运行查询。

But - I want to test the behavior of DeferredLoadingEnabled property and I need to set it via the context variable. 但是 - 我想测试DeferredLoadingEnabled属性的行为,我需要通过上下文变量设置它。

eg 例如

ctx.DeferredLoadingEnabled = false;

But how can I access ctx ? 但是我如何访问ctx

linqpad generates it for me , and I need to access it. linqpad为我生成它,我需要访问它。

In Entity Framework 5, the equivalent property is: 在Entity Framework 5中,等效属性是:

ctx.Configuration.LazyLoadingEnabled = false;

If you're in LINQPad, you're already in the context, so you can just say: 如果你在LINQPad中,那么你已经在上下文中,所以你可以说:

Configuration.LazyLoadingEnabled = false;

But when I'm copying code from Visual Studio to LINQPad, I'll often just add a line at the top so all the code works the same: 但是当我将代码从Visual Studio复制到LINQPad时,我通常只是在顶部添加一行,所以所有代码都是一样的:

var ctx = this;

When using Linqpad, you are acually inside the ObjectContext. 使用Linqpad时,您实际上 ObjectContext中。 Just type: 只需输入:

this.

and you can access the properties on your ObejctContext. 你可以在你的 ObejctContext访问属性。

(also: make sure you use "C# Statement") (另请:确保使用“C#Statement”)

There is no DeferredLoadingEnabled property in Entity Framework - it is part of Linq to SQL , not Linq to entities (EF). Entity Framework没有DeferredLoadingEnabled属性 - 它是Linq to SQL一部分,而不是Linq to entities (EF)。

Entity Framework has deferred loading by default. 实体框架默认延迟加载。 The linked tables will be loaded on access or when you explicitly load them yourself or when you load them directly with the first resource (eager loading). 链接表将在访问时加载,或者当您自己显式加载它们或直接使用第一个资源加载它们时(加载加载)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM