Accessing a collection on a new entity results in database access

Posts   
 
    
Posts: 98
Joined: 10-Nov-2006
# Posted on: 24-Mar-2012 00:56:16   

This is with LLBLGen 3.5, Self Servicing.

While using ORMProfiler, I noticed that if I run code like this:


XEntity x = new XEntity(); // Create a new X
YEntity y = new YEntity(); // Create a new Y
x.GetMultiYs(false).Add(y); // Add a new Y Entity to the Y collection

// set properties on x, y

x.Save(true);


then as soon as the code calls GetMultiYs, there is a database fetch of all Y entities related to an XEntity where XId = 0.

I was surprised that accessing a collection on a new entity would cause a database access, since a new entity can't have related entities. I'd like to avoid that fetch for performance reasons if possible.

I've found that I can work around the issue by setting x.AlreadyFetchedYs = true, but I'm wondering if that's necessary. In my real code I will have quite a large number of AlreadyFetchedXXX's properties to set.

Or perhaps I've missed something and there's a different pattern of code I should be using to avoid the fetch in cases like this.

Thanks for any advice,

Wesley

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Mar-2012 07:27:12   

Hi Wesley,

It's the nature of SelfServicing (lazy loading). There is a lot of discussion about this: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=2013 At the end of the day you need that property to ensure the related collection is not fetched that time. This is also briefly addressed in the documentation.

If you want a disconnected pattern, you can use Adapter instead of SelfServicing.

David Elizondo | LLBLGen Support Team