I have the following annotated model
public class TypeA
{
public int TypeAId { get; set; }
[Required]
public TypeB B { get; set; }
public string AValue { get; set; }
}
public class TypeB
{
public int TypeBId { get; set; }
public string BValue { get; set; }
}
exposed as v3 odata by a WCF Data Service using entity framework. When i attempt to update a TypeA using a DataServiceContext such as
var ctx = new Service.Context(new Uri("http://localhost/TestUpdateService/TestUpdateService.svc"));
var t = ctx.theATypes.Expand(p => p.B).First();
t.AValue = "New value";
ctx.UpdateObject(t);
ctx.SaveChanges();
I get a DbEntityValidationException in the service stating "The B field is required"
the body of the request "MERGE /TestUpdateService/TestUpdateService.svc/theATypes(1) HTTP/1.1" contains the AValue property change but does not contain any of the link information to property B (which is my guess as to why the validation is failing in the service). Am i missing something about updating the data service?
I believe what's happening is that OData uses MERGE verb that is more efficient than PUT (PUT requires sending all fields while MERGE sends only changed data), but in your model field "B" is marked as Required, so you get a validation exception on the client side. To test that this is the case you can temporarily remove [Required] attribute from the "B" field and check that the update operation succeeds. If so, you have two options: