Entity Framework lazy loading doesn't work from other thread

Posted by Thomas Levesque on Stack Overflow See other posts from Stack Overflow or by Thomas Levesque
Published on 2010-04-04T21:39:06Z Indexed on 2010/04/04 21:43 UTC
Read the original article Hit count: 471

Hi,

I just found out that lazy loading in Entity Framework only works from the thread that created the ObjectContext. To illustrate the problem, I did a simple test, with a simple model containing just 2 entities : Person and Address. Here's the code :

    private static void TestSingleThread()
    {
        using (var context = new TestDBContext())
        {
            foreach (var p in context.Person)
            {
                Console.WriteLine("{0} lives in {1}.", p.Name, p.Address.City);
            }
        }
    }

    private static void TestMultiThread()
    {
        using (var context = new TestDBContext())
        {
            foreach (var p in context.Person)
            {
                Person p2 = p; // to avoid capturing the loop variable
                ThreadPool.QueueUserWorkItem(
                    arg =>
                    {
                        Console.WriteLine("{0} lives in {1}.", p2.Name, p2.Address.City);
                    });
            }
        }
    }

The TestSingleThread method works fine, the Address property is lazily loaded. But in TestMultiThread, I get a NullReferenceException on p2.Address.City, because p2.Address is null.

It that a bug ? Is this the way it's supposed to work ? If so, is there any documentation mentioning it ? I couldn't find anything on the subject on MSDN or Google...

And more importantly, is there a workaround ? (other than explicitly calling LoadProperty from the worker thread...)

Any help would be very appreciated

PS: I'm using VS2010, so it's EF 4.0. I don't know if it was the same in the previous version of EF...

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework