Hello,
I need to return null to my code if fetching an entity from the database was unsuccessful due to an invalid primary key; No record with the primary key exists. I found a few postings, but the suggestions didn't work. In the end, I implemented the following code. Is it the proper way to detect that a fetch of an entity resulted in a valid entity? If the customerId does not exist, I need to return a null. If the fetch was successful due to an existing customerId, I need to return the fetched entity.
Thank you for your help,
Mike
var entity = new CustomerEntity(customerId);
Adapter.FetchEntity(entity);
// The boolean result of adapter.FetchEntity represents the following:
// True: the Fetch was successful (whether or not the row exists in DB).
// So you can receive true and still the entity is IsNew=true.
//
// False: the Fetch was not successful (whether or not the row exists in DB).
// If you receive false maybe there is a problem with the query, maybe there
// is an authorization restriction.
//
// So, check the IsNew property of the entity.
// If after fetch IsNew=false, then the row exists on DB,
// If IsNew=true, the row doesn't exits in DB so the entity automatically is set as new.
return ((entity.IsNew && entity.Fields.State == EntityState.OutOfSync) ? null : entity);