Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:20:59+00:00 2026-06-04T17:20:59+00:00

I have the following test case [TestMethod()] [DeploymentItem(Courses.sdf)] public void RemoveCourseConfirmedTest() { CoursesController_Accessor target

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:21:01+00:00Added an answer on June 4, 2026 at 5:21 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Linq to NHibernate. I have a following test case : [TestMethod]
I have the following test case: include_once('../Logger.php'); class LoggerTest extends PHPUnit_Framework_TestCase { public function
When I have the following auto test case : class MyException: virtual public boost::exception,
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have the following code: private void SetControlNumbers() { string controlString = ; int
I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase):
I have the following string - Default (45-889) %69 (Test Case)-1/24/2012 12:16:01 AM-1/24/2012 12:16:11
I have the following code in a test case on jsPerf: var arr =
I have the following test case in eclipse, using JUnit 4 which is refusing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.