|
Rosacek
User
Location: CZ
Joined on: 18-Mar-2012 18:02:44
Posted: 133 posts
|
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
|
|
|
|
daelmo
Support Team
Location: Guatemala City
Joined on: 28-Nov-2005 23:35:24
Posted: 8086 posts
|
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
|
|
|
Otis
LLBLGen Pro Team
Location: The Hague, The Netherlands
Joined on: 17-Aug-2003 18:00:36
Posted: 37632 posts
|
|
|
jovball
User
Location: USA
Joined on: 23-Jan-2005 19:53:47
Posted: 393 posts
|
|
|
Rosacek
User
Location: CZ
Joined on: 18-Mar-2012 18:02:44
Posted: 133 posts
|
Thanks for additional info R.
|
|
|
|