私は、アプリをデータベースにリンクする Entity Framework を使用して、小さな ASP.NET MVC4 Web アプリに取り組んでいます。コントローラーとビューですべての CRUD 操作を Visual Studio 2012 に生成させると、編集以外はすべて正常に機能します。
私の生成されたオブジェクトは次のとおりです。
public partial class Post
{
public Post()
{
this.Attachment = new HashSet<Attachment>();
}
[Display(AutoGenerateField = false)]
public int post_id { get; set; } // This is the PK of my table
public string title { get; set; }
public string text { get; set; }
[Display(AutoGenerateField = false)]
public System.DateTime created { get; set; }
[Display(AutoGenerateField = false)]
public Nullable<System.DateTime> modified { get; set; }
[Display(AutoGenerateField = false)]
public string author { get; set; } // This is a FK from the "author" table
public virtual ICollection<Attachment> Attachment { get; set; }
public virtual Author Author1 { get; set; }
}
Edit POST 操作は次のとおりです。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Post post)
{
if (ModelState.IsValid)
{
post.modified = DateTime.Now;
db.Entry(post).State = EntityState.Modified;
db.SaveChanges(); // DbUpdateConcurrencyException thrown here
return RedirectToAction("Index");
}
ViewBag.author = new SelectList(db.Author, "author1", "password", post.author);
return View(post);
}
編集した投稿を送信すると、ASP.NET は DbUpdateConcurrencyException を吐き出し、操作が予期しない行数 (0) に影響したことを伝えます。
デバッグ中に、author、created、および post_id が null であることがわかりました。それらはすべてその値を保持する必要があります。
どうすればこれを修正できますか?
前もって感謝します。