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