无法使用 LINQ to SQL 保存对具有外键约束的子表的更改
我正在尝试自学 LINQ to SQL,并决定做一个小型 MVC.NET 项目来入门。问题是我很早就犯了以下错误。
我正在制作一个高尔夫应用程序,因此我已经设置了数据库并制作了我的 dbml 类。该数据库有一个course
表和一个hole
表。 hole
表通过其主键引用 courseId
并对其具有外键约束。所以一切都很标准。
我的控制器上有一个 MVC 操作,允许我编辑球场信息(名称和洞信息,例如标准杆和行程索引等)。
当我在编辑后进行保存时,出现以下错误。
“尝试删除球场和球洞之间的关系。但是,该关系的外键之一 (Hole.CourseId) 不能设置为 null。”
我已经对此进行了调试,没有发现任何问题。我需要在数据库上执行某些操作才能编辑具有外键约束的表吗?
这是操作代码:
[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);
}
}
其中 _repository 如下所示:
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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新模型有什么作用?假设它对视图模型上的 Course 成员进行了更改,您可能需要考虑如下存储库方法:
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: