I have Backpack and Book entities. Book references Backpack (one to many). I am creating an instance of Backpack and bunch of Books. So in this case backpack has bunch of books. I am saving those entities to the db. I am verifying that those got saved to the db. When I try to load backpack it loads fine and all the properties are set except the navigation properties. I am also checking that LazyLoading is not disabled. My navigation properties has the virtual keyword. I am not sure what I am doing wrong. If I try to Load the backpack with Include() it loads the books:
dbContext.Backpacks.Where(b=>b.Name!="").Include("Books").FirstOrDefault()
I am trying to figure out why it is not loading the books lazily? I have the same problem with loading the book. When I load the book, it doesn't have the backpack attached. I see that the BackpackId is there.
In my property getter/setter I have some logic that will be fired, but I am not sure how that could be a problem.
With the limited information at hand, I can see the following explanations for your problem.
Make sure that lazy loading and proxy creation are enabled, the first doesn't work without the latter.
dbContext.Configuration.ProxyCreationEnabled = true;
dbContext.Configuration.LazyLoadingEnabled = true;
(see this SO post for details)
Simplified, Lazy Loading works like this:
virtual navigation properties, which is the proxy.This last step is, of course, only possible if the entity/proxy is still attached to the context and can therefore query the database to retrieve said objects.
using( var dbContext = new MyContext() )
{
dbContext.Configuration.ProxyCreationEnabled = true;
dbContext.Configuration.LazyLoadingEnabled = true;
// Note: You can modify or derive from `MyContext` so that this is
// done automatically whenever a new `MyContext` is instantiated
var backpack = dbContext.Backpacks.Where(b=>b.Name!="").FirstOrDefault();
// This should work
var firstBook = backpack.Books.FirstOrDefault();
}
// This will probably not, because the context was already disposed
var firstDrink = backpack.Drinks.FirstOrDefault();
I hope this helps, but feel free to provide more information if it doesn't
After a several days of debugging, finally figured out the problem. As people mentioned above, you have to enable the LazyLoading and ProxyCreating. I had the issues even after having the enabling the LazyLoading and ProxyCreating. Also make sure you declare your navigation properties as virtual, otherwise EF will not be able to load entities lazily.
So the issue I had was, EF wasn't crating Proxies because of my entity didn't have a public or protected constructor with NO parameters. After creating public (in my case protected) constructor without parameter it worked.
NOTE: Not having public/protected constructor without parameters will not affect the eager loading.
Here is a link that explains the requirements for the LazyLoading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With