Print Page | Close Window

Create Entity with Child Still Fires Lazy Load on Child Collection

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1510
Printed Date: 20-May-2025 at 9:55pm


Topic: Create Entity with Child Still Fires Lazy Load on Child Collection
Posted By: skingaby
Subject: Create Entity with Child Still Fires Lazy Load on Child Collection
Date Posted: 06-Oct-2009 at 1:41pm
I have a standard Parent Child Entity setup, let's say: Order and OrderDetail. I use the Create method pattern shown in the Model Explorer for creating entities.

When I create an Order, I create a blank OrderDetail entity to go in it. This is then shown in my grid. When that OrderDetail is bound to the grid, the EntityManager fires a lazy load query back to the server trying to pull the OrderDetails entities for Order ID -100. How do I turn this off? At this point, there are NO OrderDetails in the database and this query is completely pointless. Is there a "Loaded" property or some such that I can set to suppress it?

Thanks, Simon

Here's the code for the Create methods I am using:

public partial class Order
{
     public static Order Create(User user)
     {
          InitManager();
          Order item = Manager.CreateEntity<Order>();
          GetATemporaryKey(item);
          AddToManager(item);
          item.InitializeChildren(user);
          return item;
     }

     protected override void InitializeChildren(User user)
     {
          OrderDetail.Create(this, user);
     }
}

public partial class OrderDetail
{
     public static OrderDetail Create(Order order, User user)
     {
          InitManager();
          OrderDetail item = Manager.CreateEntity<OrderDetail>();
          GetATemporaryKey(item);
          AddToManager(item);
          item.Order = order;
          return item;
     }
}



Replies:
Posted By: kimj
Date Posted: 07-Oct-2009 at 9:31am
Unfortunately there's no easy way to suppress the load.  You can set the EntityReferenceStrategy on an EntityRelationLink to apply globally -- e.g., any time you're navigating from Order to OrderDetails -- but that's probably not what you want here.  We'll look into fixing a few things here -- 1) not trying to load a child list for a new parent, and 2) allowing the EntityReferenceStrategy to be set at a more granular level.
 
Right now, using the EntityManager.Querying event may be the best approach, although still clumsy since you need to look for an EntityRelationQuery querying in the direction wanted, then look for temporary IDs, and then cancel the query.
 
entityManager.Querying += (o,e) => {
   var rq = e.Query as EntityRelationQuery;
   if (rq == null) return;
   if (rq.RelationLink == EntityRelations.FK_Order_Details_Orders.ToRole2Link) {
      foreach (OrderSummary order in rq.Entities) {
         if (order.Id <= 0) {
            e.Cancel = true;
         }
      }
   }
};


Posted By: skingaby
Date Posted: 07-Oct-2009 at 12:25pm
I am just a fountain of bugs and features, aren't I?
I'll see if I can implement this in a way that will handle our entities. Thanks Kim!



Print Page | Close Window