Entity Framework Lazy Loading in .NET 3.5

2019-05-04 03:29发布

问题:

Due to server restrictions I am limited to .Net 3.5, I was using lazy loading with Linq to SQL but have since switched to the Entity Framework. L2E does not have lazy loading in 3.5 while L2S did. Is there a way to regenerate the templates somehow to achieve this?

回答1:

You have to explicitly call a load method in EF 1 / .NET 3.5.

So, before you access a related collection or entity that is not loaded, you have to call something like:

Examples:

if (!person.Pets.IsLoaded)
    person.Pets.Load();
if (!person.Address.IsLoaded)
    person.Address.Load();

Of course it's so ugly, but this is how it worked in that version.

More details from Microsoft Blogs here:

http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/20/entity-framework-and-lazy-loading.aspx