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

  • Home
  • SEARCH
  • 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 956647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:34:21+00:00 2026-05-16T00:34:21+00:00

I am trying to teach myself LINQ to SQL and have decided to do

  • 0

I am trying to teach myself LINQ to SQL and have decided to do a small MVC.NET project to get my feet wet. The problem is that I have fallen very early on with the following error.

I am making a golf application so I have setup the database and got my dbml classes made. The database has a course table and a hole table. The hole table references the courseId by its primary key and has a foreigh key constraint on it. So all pretty standard.

I have an MVC action on my controller that allows me to edit the course information (name, and hole info, for example, par and stroke index, etc.)

When I do the save after an edit I get the following error.

“An attempt was made to remove a relationship between a Course and a Hole. However, one of the relationship’s foreign keys (Hole.CourseId) cannot be set to null.”

I have debugged this and can’t see any problems. Is there something I need to do on the database to allow me to edit tables with foreign key constraints?

Here is the action code:

    [HttpPost]
    [ValidateInput(true)]
    public ActionResult Edit(int id, FormCollection collection)
    {
        var courseViewModel = new CourseViewModel { Course = _repository.GetCourse(id) };

        if (courseViewModel.Course == null)
        {
            return View("NotFound", string.Format("Course {0} Not Found", id));
        }

        try
        {
            UpdateModel(courseViewModel);
            _repository.SubmitChanges();

            return RedirectToAction("Index", "Course");
        }
        catch (Exception ex)
        {
            ModelState.AddRuleViolations(courseViewModel.Course.GetRuleViolations());
            ModelState.AddModelError("", ex.Message);

            return View(courseViewModel);
        }
    }

Where _repository looks like this:

    namespace Web.Repository
    {
        public class MyRepository
        {
            private MyDataContext db = new MyDataContext();

            public void SubmitChanges()
            {
                db.SubmitChanges();
            }

            public Course GetCourse(int id)
            {
                return db.Courses.SingleOrDefault(i => i.CourseId == id);
            }
        }
    }
  • 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-05-16T00:34:21+00:00Added an answer on May 16, 2026 at 12:34 am

    What does UpdateModel do? Assuming it makes changes to the Course member on your view model, you might want to consider a repository method like this:

    public void SaveCourse(Course course)
    {
      db.Courses.Attach(course);
      db.SubmitChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to teach myself SQL. I have a web matrix project I
I'm working with the NerdDinner application trying to teach myself ASP.NET MVC. However, I
In trying to teach myself ASP.NET and OO design in general, I decided to
I'm currently trying to teach myself PHP by doing a small image-processing type project
I've been trying to teach myself Android, and have just learned that I've been
I'm trying to teach myself some C++ and as an initial project, I want
I am trying to teach myself C from a python background. My current mini-problem
I am trying to teach myself Python, and I have realized that the only
I'm trying to teach myself CSS and have the following markup: <style type=text/css> #content
I'm just trying to teach myself how to use Linq. This is what I

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.