Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Change Entity PK value with collection inside
 

Pages: 1
LLBLGen Pro Runtime Framework
Change Entity PK value with collection inside
Page:1/1 

  Print all messages in this thread  
Poster Message
OSSistemes
User



Location:

Joined on:
20-Nov-2019 21:55:31
Posted:
9 posts
# Posted on: 22-Jan-2020 09:49:34.  
Hi, we have a little problem, we are newbe and learn all about llblgen as possible, but we don't find a solution for this.

We a have a entity with PK and another entity with FK:

Code:


OrderEntity test = new OrderEntity();
test.FetchUsingPK(100);
//Order have a relation with OrderLines

foearch (OrderLinesEntity line in test.OrderLinesEntity)
{
Console.WriteLine ( line.ordernumber);
}
//Well all ok, this order have two lines, and print order number for twice

//now we change pk
test.ordernumber = 200; //ordernumber is PK

foearch (OrderLinesEntity line in test.OrderLinesEntity)
{
Console.WriteLine ( line.ordernumber);
}
//Print number 100 twice, not number 200

test.Save();
//Entity is saved with new number 200, and orderlines are FK and saved correcty too with //number 200 not number 100.




The question is that if I change entity PK how its possible to change all relationship PK-FK in all object?

Thanks
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14637 posts
# Posted on: 22-Jan-2020 15:39:56.  
Orderlines were fetched from the database either by a prefetchPath or a by LazyLoading (SelfSerivce specific).

Changing the value of the PK value will not re-fetch them again, unless you fetch them explicitly.

P.S. As a Design recommendation: PK values should not be changed.
If you are using a unique field as a PK, and have a use case where it can be changed, then it should have not been used as a PK.
e.g. in the example you have mentioned, OrderNumber should have been a unique field, not a PK, since it represents a logical value that might be changed.
Hence you need to introduce an artificial key (an auto incremental number or a unique identifier).


  Top
OSSistemes
User



Location:

Joined on:
20-Nov-2019 21:55:31
Posted:
9 posts
# Posted on: 22-Jan-2020 21:18:07.  
Thanks Walaa for the comment.

Please could you tell me which is the best option to reload all object from database, I use this code, but doesn't work

Code:


OrderEntity test = new OrderEntity();
test.FetchUsingPK(100);
foearch (OrderLinesEntity line in test.OrderLinesEntity)
{
Console.WriteLine ( line.ordernumber);
}

test.ordernumber = 200;
test.Save(); //Database is saved correctly, OrderEntity and OrderLinesEntity
test.Refetch();

foearch (OrderLinesEntity line in test.OrderLinesEntity)
{
Console.WriteLine ( line.ordernumber);
}
//Print number 100 twice, not number 200



output console:

100
100
100
100

Thanks
  Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38081 posts
# Posted on: 23-Jan-2020 09:41:34.  
Refetch refetches only the entity you call it on, so in your code only 'test' is refetched. This is done automatically for you after a save, you don't have to call 'refetch' in normal cases.

With a PK value change, you have to see the change as a delete+insert, they're actually new entities (as the identity changed). This means refetching the data in the same objects isn't recommended.

If you still want to, you can: you're relying on lazy loading which doesn't get triggered again when you touch the property the second time. You can reset this, so you can do:

//...
test.Save(true); // this will make sure related entities are saved too!
test.AlreadyFetchedOrderLines = false;
// you can now lazy load test.OrderLines again.

However be careful with lazy loading. It's easy to run into a select n+1 where you issue a lot of queries which is unnecessary. Always first consider a prefetch path (eager loading) before using lazy loading. https://www.llblgen.com/Documentation/5.6/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/SelfServicing/gencode_prefetchpaths.htm

Also, if you are just starting with your project, please consider Adapter over SelfServicing. If this is for a project you inherited which already uses selfservicing, you can keep using it of course (it won't go away, but it doesn't have some advanced features as adapter does).


Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.