I’ve been trying to get why this isn’t working.
I have a web service with a number of methods. One of them returns an element of an entity, and another one expects an element of that same entity and is supposed to update the context and the database with the changes made on the client side.
the “get” code is as follows
public InvoiceHeader getHeader(long pIdHeader){
try{
InvoiceHeader ih = lContext.InvoiceHeaders.SingleOrDefault(x => x.idHeader == pIdHeader);
return ih;
} catch (Exception exp){
//log error
}
}
I’ve tried several approaches to get this working. Here are the ones I think are closest to the answer :s
(1):
public bool submitChanges(InvoiceHeader iHeader){
try{
InvoiceHeader ih = getHeader(iHeader.idHeader);
lContext.InvoiceHeaders.Context.Refresh(RefreshMode.KeepChanges, iHeader);
lContext.SubmitChanges();
return true;
} catch (Exception exp) {
//log error
return false;
}
}
the other approach was
(2)
public bool submitChanges(InvoiceHeader iHeader){
try{
lContext.InvoiceHeaders.Attach(iHeader, true);
lContext.SubmitChanges();
return true;
} catch (Exception exp) {
//log error
return false;
}
}
In both cases it all ran ok to “return true”, but no update was made. Please advise on what am I doing wrong here.
If you need any more information, let me know.
Thanks all
Ricardo.
Solved.
As it turns out, when we get an entity out of the context’s control, it will no longer control it, even if we attach it to the context. Unless, that is, if we have a is version field;
To get it working,
=> add a version field to your entity;
1) check Nullable checkbox
2) Is Version checkbox
3) Update check = Never
4) Auto-Sync Always;
You should handle the version somehow. Perhaps an on update trigger;
Code:
1) create a new instance of your context;
2) attach the object, telling linq it was changed
3) submit changes
The GREAT BIG difference to what I had already tried before really is the version field on my entity.