Could this be a bug?

Go To StackoverFlow.com

0

I have the following test case

    [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));

    }

Which tests the following method from my controller.

    [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);
        }

    }

I know i should be using a Mock db .... but for this project I have decided to go with this approach.

So this what happens. When I run the actual web site this function works perfectly fine and removes the course and all the meets that belong to it.

But when I run the test i get the following exception

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.

Here is the even more interesting part if I comment out the following line from the test

List<CourseMeet> meets = courseToDelete.meets.ToList<CourseMeet>();

and replace the loop with the following:

foreach (var meet in db.Meets.ToList())
{
    Assert.IsFalse(meet.courseID == courseToDelete.courseID);
}

I dont get any exceptions and the test case passess.

Am I missing something about Entity Framework or is this a bug?

2012-04-05 18:28
by keshav
Any child tables to Courses? As far as I know, EF doesn't delete child entities by default but instead set their foreign key to parent to null. Looks like that on the error message - magnus 2012-04-05 18:39
That's what I have found on the Internet. But when I run the actuall application it delete the course and it's children (couse meets) from the DB. And also the test passes when I remove the line - keshav 2012-04-05 18:55


0

Well this has been open for a while now. I still haven't been able to find a definite answer but working more with MVC and EF i think what is happening is that once i execute the line

List<CourseMeet> meets = courseToDelete.meets.ToList<CourseMeet>();

the meets get loaded into the object manager and hence when the parent object is deleted the no longer have a reference to the parent course.

2012-05-31 02:54
by keshav
Ads