shekar wrote:
But still my question remains unanswered. The code earlier I had written updates the field narration even without setting ischanged property. Why only banker_id does not update ?
The normal entity life cycle works like this:
- You fetch the entity
- You make changes to entity
- You save back
Example:
// fetch entity
var ledgerId = Convert.ToInt32(GlobalVariable.searchledgerid);
var ledger = new LedgerEntity(ledgerId);
adapter.FetchEntity(ledger);
// make changes
ledger.BankerId = ValidateUI.GetComboboxNullable(cmbbankerid.SelectedValue);
// save it back
bool succeeded = adapterledger.SaveEntity(ledger);
If, for some reason you don't want to fetch the entity but you still want to update an existing record on your db, you can do the IsNew=false trick. This has the downside of the update fields to null. In the normal behavior (entity life cycle) this isn't a problem as if the field is not null, when you set to null, a change is detected and the column is updated.
In your approach, you set the field to null, but the field is already null because you didn't fetch it, so you have to do the IsChanged trick mentioned by Walaa:
// IsNew trick (without fetch)
var ledger = new LedgerEntity();
ledger.LedgerId = Convert.ToInt32(GlobalVariable.searchledgerid);
// make changes (use IsChanged trick when set to null)
ledger.BankerId = ValidateUI.GetComboboxNullable(cmbbankerid.SelectedValue);
ledger.Fields["BankerId"].IsChanged = true;
// save it back
bool succeeded = adapterledger.SaveEntity(ledger);
shekar wrote:
Secondly, do I have to set is changed for every field which is being updated? it results in lengthy code right !
You just need to do IsChanged=true if the new value of the field is null and you are using the second approach (don't fetch the entity first).
In the documentation there is a note as well about this:
Setting a field to the same value it already has will not set the field to a value (and will not mark the field as 'changed') unless the entity is new.
Which is your case, as the value is null at first (you didn't fetch it so the original value is unknown), then you set it to null, so no change is detected.
Hope that makes sense to you