rem
rem

Reputation: 17065

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 trying 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?

Upvotes: 2

Views: 2971

Answers (2)

rem
rem

Reputation: 17065

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.

Upvotes: 2

dlev
dlev

Reputation: 48596

In your OnChangesSaved handler, you need to call context.EndSaveChanges(result).

This link has some sample code that illustrates the whole process.

Upvotes: 0

Related Questions