我必须更新我的实体和它的子列表,如下所示:
public class Entity1
{
int Id{get;set;}
ObservableCollection<Child1> ChildrenList {get;set;}
string Name{get;set;}
}
public class Child1
{
string Nome{get;set;}
string Cognome {get;set;}
}
我是这样实现patch方法的:
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Entity1> entityDelta)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var entity = await Context.Entity1.FindAsync(key);
if (entity == null)
{
return NotFound();
}
entityDelta.Patch(entity);
try
{
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
return Updated(entity);
}
但是当我尝试用这种方式从fiddler调用它时:
Url: http://localhost/odata4/Entity1(1)/ with patch request
Request Headers: Content-Type: application/json
Request Body:
{
Name: "pippo2",
ChildrenList:[{
Nome: "Test",
Cognome: "Pippo"
}]
}
它给出了一个错误的模型。isValid属性和指定返回此错误:
不能对实体上的导航属性'ChildrenList'应用PATCH类型"Entity1"。
我怎么解决它?贴片法是正确的方法吗?
OData V4规范说要更新实体:
实体绝对不能包含相关实体作为内联内容。它可能包含导航属性的绑定信息。对于单值导航属性,这将取代关系。
所以,你可以使用:更新子节点:
补丁/把:~/Child1s(…)
更新父
补丁/把:~/Entity1s(…)
更新父级和子级的关系:
补丁/将~/Entity1s(…)/ChildrenList/$ ref
带有实体链接的内容