Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > Bugs & Issues> Saving OutOfSync entity
 

Pages: 1
Bugs & Issues
Saving OutOfSync entity
Page:1/1 

  Print all messages in this thread  
Poster Message
Rosacek
User



Location:
CZ
Joined on:
18-Mar-2012 18:02:44
Posted:
133 posts
# Posted on: 05-Jan-2018 13:31:58.  
Adapter, LLBLGEN Pro v.4.2.16.929

Hi,
I wonder what is the correct way of doing following:
Say I need to save comment "xyz" to company "ACME"

I don't know if such company exists in DB, therefore I try to fetch it first by using UC
If exists then I simply set .Comment="xyz" and call adapter.SaveEntity

But when such entity doesn't exists in DB yet, then fetched entity is OutOfSync.
And this is the point.
Is it ok just set .Comment="xyz" and call adapter.SaveEntity?

Or do I have to create new instance of CompanyEntity, set its fields CompanyName="ACME" and Comment="xyz" and then save?

Years I thought I have to create new instance as not-found fetched entity is OutOfSync, but today I noticed, it isn't neccessary. Even Entity is out of sync, I can save it when it is new

Is this code OK both both cases, entity exists/not exists in DB ?
Code:

Dim customer As New CustomerEntity()
Using adapter As New DataAccessAdapter()
     customer.CompanyName = "ACME"
     adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCCompanyName())

     customer.Comment = "xyz"
     adapter.SaveEntity(customer)
End Using


Or better to use this way:
Code:

Dim customer As New CustomerEntity()
Using adapter As New DataAccessAdapter()
     customer.CompanyName = "ACME"
     If NOT adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCCompanyName()) THEN
         customer = New CustomerEntity()
         customer.CompanyName = "ACME
     END IF
     customer.Comment = "xyz"
     adapter.SaveEntity(customer)
End Using
  Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8088 posts
# Posted on: 06-Jan-2018 07:42:09.  
1st approach is ok and simpler. One more thing, The OutOfSync state happens when the data in your entity is not in sync with the data en your DB. To overcome this, if you need to use the entity later (read the fields, etc), you need to fetch it again. You can do this in the SaveEntity method:
Code:
adapter.SaveEntity(customer, true);

You also could flag the framework to this automatically every time, via EntityBase2 object or via the config file. For more info see Entity State


David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
37645 posts
# Posted on: 06-Jan-2018 08:28:53.  
Additionally to what David said, for an individual entity object in memory, you can set the State property of the Fields object of an entity to 'Fetched'. WHen an entity is 'outofsync' the state is set to 'OutOfSync' hence the error. (https://www.llblgen.com/Documentation/5.3/ReferenceManuals/LLBLGenProRTF/html/34138BD2.htm)
Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
jovball
User



Location:
USA
Joined on:
23-Jan-2005 19:53:47
Posted:
393 posts
# Posted on: 08-Jan-2018 22:20:05.  
I had a similar question last year and solved it to my satisfaction. You might want to look at this thread and see if anything said by daelmo or myself there applies to your situation.

http://llblgen.com/TinyForum/Messages.aspx?ThreadID=23968


Joel Reinford
LLBLGenPro Version: 5.5.3
Templates: Adapter
Framework: .Net 4.6
Database: SQL Server 2012/2014, DB2 v10
 
Top
Rosacek
User



Location:
CZ
Joined on:
18-Mar-2012 18:02:44
Posted:
133 posts
# Posted on: 08-Jan-2018 22:22:41.  
Thanks for additional info
R.
  Top
Pages: 1  


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

Version: 2.1.12172008 Final.