FK-PK Syncronization

Posts   
 
    
rihadd
User
Posts: 22
Joined: 19-Sep-2007
# Posted on: 29-Oct-2009 18:21:11   

Version 2.6 Final (July 27th 2009) Self Servicing

Hi, I have an issue with adding child entires to a newly created parent entity. Let's for illustrative purposes say I have a Customer entity that has 1:1 relation to an Order enity which in turn has a 1:M relation to LineItems. All of the PK in the database are of NEWSEQUENTIALID type and I have set the SqlServerDQECompatibilityLevel to 2.

I fetch a Customer which doesn't have any orders, access Customer.Order (I have the 'create new order if doesn't exist' flag set to true) and start adding new LineItems to the Order.LineItems collection. What I'm seeing is that the LineItems collection does not grow, the count is always 0. Shouldn't I be able to add the child entities? Do I have to save the Order entity before I start adding LineItems?

LineItemEntity myEntiy = new LineItemEntity(); Customer.Order.LineItems.Add(myEntity); LineItems.Count would return 0 at this point.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 29-Oct-2009 21:42:21   

Can you try...


OrderEntity order = customer.order;
LineItemEntity myEntiy = new LineItemEntity();
LineItemEntity myEntiy2 = new LineItemEntity();
order.LineItems.Add(myEntity);
order.LineItems.Add(myEntity2);

and then check the order.LineItems count (should be 2).

Thanks

Matt

rihadd
User
Posts: 22
Joined: 19-Sep-2007
# Posted on: 29-Oct-2009 23:32:58   

Yes, that works, but why do I have to create a new reference? Imagine my Customer entity is exposed to the other layers by some singleton DataService class. If, as you suggested, I create a new reference to the Order entity and add my items to it, the Customer entity in the DataService singleton will be oblivious to the changes.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Oct-2009 11:41:44   

What's the difference between your model and the following one (from Northwind):

            var order = new OrdersEntity(); // nothing to fetch here
            int count = order.OrderDetails.Count; // 0
            order.OrderDetails.Add(new OrderDetailsEntity());
            count = order.OrderDetails.Count;  // 1
rihadd
User
Posts: 22
Joined: 19-Sep-2007
# Posted on: 30-Oct-2009 13:55:23   

I have to do it the following way.

  1. LineItem my = new LineItemEnity();
  2. IDataService.Customer.Order.LineItems.Add(my);
  3. IDataService.Customer.Order.LineItems.Count;//0

Keep in mind that the Order entity does not exist in db yet (it's being created by the framework because I have the 'create new order if not present' flag set to true in Customer). If the order entity was previously persisted the count returned at 3 would be 1.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Oct-2009 07:29:48   

I can't reproduce that with the latest version. There is something special about your scenario we need to know?

Could you please download the runtime library source code and check what actually is happening? or maybe cook a tiny solution that reproduces the problem and attach it here.

David Elizondo | LLBLGen Support Team
rihadd
User
Posts: 22
Joined: 19-Sep-2007
# Posted on: 02-Nov-2009 16:38:35   

Please see the attached test project.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Nov-2009 07:15:21   

I reproduces your situation now. It seems related to your configuration (1:1 relation on a UC field, and the way you are adding the entity). I traced all the framework sourcecode to find the reason, but it seems difficult to explain in detail. What I can tell you is that the workaround posted above should work. This also should do the trick:

myAccountEntity.Customer.CustomerParameters.Add(myCPEntity);
myAccountEntity.AlreadyFetchedCustomer = true;

The thing is that the entity is added to the collection, but the customer property is marked as "not fetched", so inverting this avoid the customer to be fetched again (in the line where you ask for the Count property).

I know is not the ideal solution you are looking for, however in SelfServicing we must be aware of this kind of things so no extra queries be executed, etc. (ref...)

David Elizondo | LLBLGen Support Team