Update a record in a database using WCF Data Services
I'm trying to use WCF Data Services and I'm a little confused about where I'm wrong in tr开发者_JAVA技巧ying to update a record, based on a key field value.
My simplified approach, which doesn't work: MydbEntities context = new MydbEntities(new Uri("http://localhost:53051/Services/MydbService.svc"));
MyEntity avt = context.MyTable.Where(p => p.EntID == "val1").FirstOrDefault();
avt.FieldToEdit = 1;
context.UpdateObject(avt);
context.BeginSaveChanges(OnChangesSaved, context);
...
private void OnChangesSaved(IAsyncResult result)
{
MessageBox.Show("seems ok");//I'm getting this message, but, in fact, data in db remains unchanged
}
Please, tell me, where am I wrong?
I've found the cause of the problem. It was trivial (I feel like an idiot), there was no writing access rights set in the service initializer. There was the following:
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
...
}
after I changed it to:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
all started to work as expected. And, of course, such a broad access rule I set only for testing purposes. Later it should be restricted.
In your OnChangesSaved handler, you need to call context.EndSaveChanges(result)
.
This link has some sample code that illustrates the whole process.
精彩评论