我正在開發WebAPI v2操作方法以使用實體框架6將對象圖的更新持久化,我有點不確定我的語法是否正確。 項目是我的根類型,其中包含問題的集合。該項目將已經存在,但帖子行動可能需要提交更改項目性質,改變現有問題的,並添加新問題的。如何使用實體框架在WebAPI操作中提交對象圖
我已經在下面顯示和評論,但更新需要大量的工作時,我想,認爲有一個輔助方法,可以爲我做一些這樣的代碼:
[System.Web.Http.Route("{id:int}")]
public HttpResponseMessage Post(int id, [FromBody]Project project)
{
// Validate the submitted project
if (!ModelState.IsValid)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
// Fetch the existing project. This is here to perform row-level
// security check. The user will have logged on and this confirms the
// organisation they work for. Behind the scenes I will compound the
// query below to include a check for organisation id. This prevents
// users submitted any project id and overwriting data that isn't theirs.
// Would it make the pattern below better if this only executed a .Count()?
// Would that mean the context would then be tracking the project before
// the update?
var existingProject = _context.Projects.Include("Questions").FirstOrDefault(p => p.ProjectId == id);
if (existingProject == null)
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ModelState);
// Copy model updates to the project
var projectEntry = _context.Entry(existingProject);
projectEntry.CurrentValues.SetValues(project);
// Now work out which questions are updates or additions
foreach (var question in project.Questions)
{
// No id so must be an addition
if (question.QuestionId == 0)
{
existingProject.Questions.Add(question);
}
else
{
// Fetch the existing question so we can copy values
var existingQuestion = existingProject.Questions.FirstOrDefault(q => q.QuestionId == question.QuestionId);
if (existingQuestion == null)
{
// In a single user system finding ourselves here should not
// be possible. Ideally we'll need to do some concurrency
// when other users make updates or have some record locking
// mechanism.
existingProject.Questions.Add(question);
}
else
{
var questionEntry = _context.Entry(existingQuestion);
questionEntry.CurrentValues.SetValues(question);
}
}
}
_context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Created, project);
}
更新:我也已經意識到提交後,我甚至沒有處理刪除問題的,但看看我的解決方案,目前不會被照顧,所以我將不勝感激,被認爲是太。
我目前面臨同樣的問題。你有沒有找到一個合理的刪除方法的答案? – John
我嘗試過[GraphDiff](https:// github。com/refactorthis/GraphDiff),但決定繼續使用類似於上面給出的代碼的代碼(有些時候會交叉)支持這種開箱即用的功能,而且我的用例目前相當獨立。 – Phil
我通過在這裏創建一個自定義通用可跟蹤實體解決方案解決了我的問題http://stackoverflow.com/questions/26681390/json-net-serialise-custom-collection-with-additional-properties-and-on-add-to-浩 – John