8

I've created a DLL assembly which contains the edmx for northwind database.

I created a reference to that dll through linqpad and I do see the db and able to run queries.

But - I want to test the behavior of DeferredLoadingEnabled property and I need to set it via the context variable.

eg

ctx.DeferredLoadingEnabled = false;

But how can I access ctx?

linqpad generates it for me , and I need to access it.

Charles
  • 50,943
  • 13
  • 104
  • 142
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

3 Answers3

13

In Entity Framework 5, the equivalent property is:

ctx.Configuration.LazyLoadingEnabled = false;

If you're in LINQPad, you're already in the context, so you can just say:

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:

var ctx = this;
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • But if I set it to false - it doesnt yield the orders ... http://i.stack.imgur.com/Nu0Y5.png . how can I make it show the orders ? – Royi Namir Sep 24 '13 at 18:11
  • @RoyiNamir: Disabling Lazy Loading is not the same as forcing Eager Loading. See http://stackoverflow.com/a/3730555/120955 – StriplingWarrior Sep 24 '13 at 18:15
5

When using Linqpad, you are acually inside the ObjectContext. Just type:

this.

and you can access the properties on your ObejctContext.

(also: make sure you use "C# Statement")

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
  • just a sec :) im booting up my machine – Jens Kloster Sep 24 '13 at 17:38
  • 1
    hmm my EF model doesn't have `DeferredLoadingEnabled` are you sure it shoud be directly on the ObejctContext - maybe is in the `ContextOptions` property – Jens Kloster Sep 24 '13 at 17:47
  • http://books.google.co.il/books?id=t1de8nSVYnkC&pg=PA362&lpg=PA362&dq=%22You+can+make+EF+behave+like+L2S+and+have+it+populate%22&source=bl&ots=24udQsLg3H&sig=boyr3QG0Lb6K_igKU8kQjDqPRUg&hl=en&sa=X&ei=5NBBUrK5L9DasgbYt4CQAw&redir_esc=y#v=onepage&q=%22You%20can%20make%20EF%20behave%20like%20L2S%20and%20have%20it%20populate%22&f=false – Royi Namir Sep 24 '13 at 17:51
5

There is no DeferredLoadingEnabled property in Entity Framework - it is part of Linq to SQL, not 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).

user2674389
  • 1,123
  • 7
  • 8
  • +1 for pointing out there is no `DeferredLoadingEnabled` property on the `ObjectContext` :) – Jens Kloster Sep 24 '13 at 17:50
  • am i blind ? http://books.google.co.il/books?id=t1de8nSVYnkC&pg=PA362&lpg=PA362&dq=%22You+can+make+EF+behave+like+L2S+and+have+it+populate%22&source=bl&ots=24udQsLg3H&sig=boyr3QG0Lb6K_igKU8kQjDqPRUg&hl=en&sa=X&ei=5NBBUrK5L9DasgbYt4CQAw&redir_esc=y#v=onepage&q=%22You%20can%20make%20EF%20behave%20like%20L2S%20and%20have%20it%20populate%22&f=false – Royi Namir Sep 24 '13 at 17:50
  • @RoyiNamir you are properly not bind. but L2S is Linq to SQL, *not* Linq to Entities – Jens Kloster Sep 24 '13 at 17:52
  • I know.but it says : "you can make EF behave like l2s"...xx.DeferredLoadingEnabled =true; maybe I dont understand the line there ?? – Royi Namir Sep 24 '13 at 17:53
  • 2
    @RoyiNamir EF behaves like L2S, when you set this property to true in L2S. If you don't set it to true, EF does not behave like it. But the sentence in the book is really written very confusing. – user2674389 Sep 24 '13 at 17:54
  • So does the incremental DB quries are done automatically **according** to the `projection` of the query || `Include` || join... ? – Royi Namir Sep 24 '13 at 17:59
  • @RoyiNamir: If you use `Include`, then the navigation properties are loaded in the same DB round-trip as the original. Likewise, if you include both the element and its property in your `Select` projection (i.e. `.Select(e => new{e, e.Things})`, the property will be populated automatically. Otherwise, as soon as you access the property, Entity Framework will attempt to load it from the database. See http://msdn.microsoft.com/en-us/data/jj574232.aspx for more details. – StriplingWarrior Sep 24 '13 at 18:12