darxis / EntityFramework.LazyLoading

LazyLoading for EF Core
MIT License
21 stars 4 forks source link

ObjectDisposedException on many to many #3

Closed voodookoop closed 7 years ago

voodookoop commented 7 years ago

Hi, Consider the following case:

        using (var dbContext = lazyFactory.Create(factoryOptions))
        {
            try
            {
                var instructorAbercrombie = dbContext.Set<Instructor>().First(x => x.LastName == "Abercrombie");

                var coursesOfinstructorAbercrombie = instructorAbercrombie.CourseAssignments.Select(x => x.Course);

                var courseCount = coursesOfinstructorAbercrombie.Count();

                Console.WriteLine($"Instructor Abercrombie has {courseCount} courses.");
            }
            catch (ObjectDisposedException)
            {
                Console.WriteLine($"{nameof(ObjectDisposedException)} occured (ConcurrencyDetector).");
            }
        }

you can reproduce the exception based on this.

voodookoop commented 7 years ago

@darxis : maybe you also figured out, that this basically has nothing to do with many to many, just simply creating another lazied dbContext, then querying a lazied prop will bring up the ObjectDisposedException.

darxis commented 7 years ago

@voodookoop Thanks for reporting an issue!

I figured out that EF Core is caching Expressions for materializing entities. In LazyLoading, such expression contains the DBContext. So the problem is that when you create another DBContext, the old materializing expressions are cahced, and they contain the old (Disposed) DBContext. I'm gonna try to fix it until the end of the day.

darxis commented 7 years ago

Closing as it is fixed in release 1.1.1-beta2, now available on NuGet.

Also note that in the new release you need to add two more lines of code to your configuration (they are present in the sample project and in the README.md)