New Posts New Posts RSS Feed: Create Entity with Child Still Fires Lazy Load on Child Collection
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Create Entity with Child Still Fires Lazy Load on Child Collection

 Post Reply Post Reply
Author
skingaby View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 23-Apr-2008
Location: United States
Posts: 146
Post Options Post Options   Quote skingaby Quote  Post ReplyReply Direct Link To This Post Topic: Create Entity with Child Still Fires Lazy Load on Child Collection
    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;
     }
}
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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;
         }
      }
   }
};
Back to Top
skingaby View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 23-Apr-2008
Location: United States
Posts: 146
Post Options Post Options   Quote skingaby Quote  Post ReplyReply Direct Link To This Post 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!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down