私は次のモデルを持っています
public class CourseModel
{
[Key]
public int courseID { get; set; }
...
public virtual ICollection<CourseMeetModel> meets { get; set; }
}
エントリの1つを編集しようとして、入力が有効な場合は正常に機能します。ただし、有効でない場合は、ユーザーに間違いを警告します。ユーザーが間違いを修正して保存しようとすると、次の例外が発生します。
ストアの更新、挿入、または削除ステートメントが予期しない行数(0)に影響しました。エンティティがロードされてから、エンティティが変更または削除された可能性があります。ObjectStateManagerエントリを更新します。
入力がコントローラーの検証手順に失敗した場合にこれが発生することに気づきました。
私のコントローラー
public ActionResult EditCourseConfirmed(CourseModel course)
{
CoursesDBContext db = new CoursesDBContext();
bool valid = validateCouse(course); //If this fails and the course model is returned back to the view I get that error
if (valid)
{
try
{
db.Entry(course).State = EntityState.Modified;
db.SaveChanges();
Session[d.s_Clear] = false;
return RedirectToAction("Index");
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, "Unable to save the course, please try again." + e.Message);
return View(course);
}
}
return View(course);
}