0
私は私のコントローラから次のような方法をテストし、次のテストケースこれはバグでしょうか?
[TestMethod()]
[DeploymentItem("Courses.sdf")]
public void RemoveCourseConfirmedTest()
{
CoursesController_Accessor target = new CoursesController_Accessor();
int id = 50;
ActionResult actual;
CoursesDBContext db = target.db;
Course courseToDelete = db.Courses.Find(id);
List<CourseMeet> meets = courseToDelete.meets.ToList<CourseMeet>();
actual = target.RemoveCourseConfirmed(courseToDelete);
foreach (var meet in meets)
{
Assert.IsNull(db.Meets.find(meet));
}
Assert.IsNull(db.Courses.Find(courseToDelete.courseID));
}
を持っています。
[HttpPost, ActionName("RemoveCourse")]
public ActionResult RemoveCourseConfirmed(Course course)
{
try
{
db.Entry(course).State = EntityState.Deleted;
db.SaveChanges();
return RedirectToAction("Index");
}
catch (DbUpdateConcurrencyException)
{
return RedirectToAction("RemoveMeet", new System.Web.Routing.RouteValueDictionary { { "concurrencyError", true } });
}
catch (DataException)
{
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again.");
return View(course);
}
}
私はモックDBを使用する必要があると知っています....しかし、このプロジェクトでは私はこのアプローチを採用することに決めました。
これはどうなりますか。実際のWebサイトを実行すると、この機能は完全にうまく機能し、コースとそれに属するすべての会合が削除されます。
しかし、私は、私がテスト
List<CourseMeet> meets = courseToDelete.meets.ToList<CourseMeet>();
から次の行をコメントアウトし、ループを交換する場合、私はここで、以下の例外
System.InvalidOperationException: The operation failed: The relationship could not be
changed because one or more of the foreign-key properties is non-nullable. When a change is
made to a relationship, the related foreign-key property is set to a null value. If the
foreign-key does not support null values, a new relationship must be defined, the foreign-
key property must be assigned another non-null value, or the unrelated object must be
deleted.
を取得し、テストがさらに興味深い部分で実行します次のようになります。
foreach (var meet in db.Meets.ToList())
{
Assert.IsFalse(meet.courseID == courseToDelete.courseID);
}
例外とテストケースの合格点はありません。
Entity Frameworkについて何か不足していますか?これはバグですか?
コースへの子テーブルはありますか?私が知る限り、EFはデフォルトで子エンティティを削除するのではなく、その外部キーを親に対してnullに設定します。エラーメッセージのように見えます。 – magnus
これは私がインターネット上で見つけたものです。しかし、私がアクチュアオール・アプリケーションを実行すると、コースが削除され、DBから子供たち(お母さんが出会う)が削除されます。また、私が行を削除するとテストに合格します。 – keshav