VS2012 単一ページ MVC Web アプリケーションに基づいてプロジェクトを生成すると、基本プロジェクトに DbEntity への参照が多数含まれます。
どうして:
db.Entry(todoList).Entity.UserId
だけではなく:
todoList.UserId
サンプル ApiControllers でこの間接化を繰り返し使用しますが、DbEntityEntry 固有のメンバー (State プロパティ) を呼び出すのは 1 回だけです。私が見落としているこれを使用する重要な理由はありますか?
編集:ここに大きなスニペットがあります
// DELETE api/TodoList/5
[ValidateHttpAntiForgeryToken]
public HttpResponseMessage DeleteTodoList(int id) {
TodoList todoList = db.TodoLists.Find(id);
if (todoList == null) {
return Request.CreateResponse(HttpStatusCode.NotFound);
}
if (db.Entry(todoList).Entity.UserId != User.Identity.Name) {
// Trying to delete a record that does not belong to the user
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}