使用DTO时的PATCH

ari*_*its 8 c# rest json-patch entity-framework-core asp.net-core

我正在研究asp.net核心webAPi和EF核心,并希望实现"更新"操作(部分编辑实体).我搜索了正确的方法来处理它,并看到我应该使用jsonPatch.问题是我通过我的API暴露了DTO,如果我使用jsonPatch:

public AccountDto Patch(int id, [FromBody]JsonPatchDocument<AccountDto> patch)
Run Code Online (Sandbox Code Playgroud)

然后我需要在DTO上应用补丁,我不能在模型实体上应用它,而不创建新的实体.

我也读过Odata.Delta,但它仍然无法在asp.net核心上运行,而且 - 我认为它没有内置的解决方案来处理dto(我发现这个例子可以帮助当Odata for core能得到的)

那么,现在 - 我应该使用POST并在查询中发送带有更改属性列表的DTO(正如我在这里看到的那样),或者 - 有更优雅的解决方案吗?

谢谢!

ari*_*its 6

现在我看到使用 autoMapper 我可以做到

CreateMap<JsonPatchDocument<AccountDTO>, JsonPatchDocument<Account>>();
        CreateMap<Operation<AccountDTO>, Operation<Account>>();
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力:)