Saving OutOfSync entity

Posts   
 
    
Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# 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 ?


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:


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

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# 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:

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 Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# 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 | Lead developer LLBLGen Pro
jovball
User
Posts: 434
Joined: 23-Jan-2005
# 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

Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# Posted on: 08-Jan-2018 22:22:41   

Thanks for additional info R.