Odata 如何使用子实体更新实体?贴片法不';行不通
我必须使用其子列表更新实体,如下所示:Odata 如何使用子实体更新实体?贴片法不';行不通,odata,asp.net-web-api-odata,odata-v4,Odata,Asp.net Web Api Odata,Odata V4,我必须使用其子列表更新实体,如下所示: 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;} } 它在Mode
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;}
}
它在Model.isValid属性中给出了一个错误,并指定返回此错误:
无法将修补程序应用于实体上的导航属性“ChildrenList”
键入“Entity1”
我怎样才能解决它?补丁方法是正确的使用方法吗 表示要更新实体:
实体不得包含作为内联内容的相关实体。它可能包含导航属性的绑定信息。对于单值导航属性,这将替换关系。对于集合值导航属性,这将添加到关系中
因此,您可以使用:
使用实体引用链接内容。如何在Fiddler中尝试?内容类型:application/json的请求主体是什么?我尝试了一下,但失败了:
{ChildrenList:[{“uri”:http://localhost/odata4/ChildrenList(1) “}]}
您可以参考一个测试用例:相关的控制器是
[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);
}
Url: http://localhost/odata4/Entity1(1)/ with patch request
Request Headers: Content-Type: application/json
Request Body:
{
Name: "pippo2",
ChildrenList:[{
Nome: "Test",
Cognome: "Pippo"
}]
}