带有实体框架和编辑多对象的MVC 3会导致“引用完整性约束违反”
本文关键字:引用 完整性约束 框架 实体 编辑 MVC 多对象 | 更新日期: 2023-09-27 18:07:08
我有一个MVC 3项目,我正在使用实体框架作为我的模型。我有一个对象"Employer",它有"Address"answers"PostalAddress",我想同时显示(查看)和更新(编辑)(即在雇主详细信息和地址详细信息同时更新的页面上)
我的视图似乎工作良好:
var employer = (from e in entities.Employers.Include("Address").Include("PostalAddress")
where e.EmployerNumber == employerNumber
select e).First();
return View(employer);
我的编辑显示得很好(即所有的文本框都填充了雇主和地址的详细信息)
[HttpPost]
public ActionResult Edit(Employer employer)
{
if (ModelState.IsValid)
{
entities.Employers.Attach(employer);
entities.ObjectStateManager.ChangeObjectState(employer, EntityState.Modified);
entities.SaveChanges();
return RedirectToAction("Index");
}
return View(employer);
}
但是当我去保存时,我在entities.Employers.Attach(employer)行上得到以下异常:
发生了引用完整性约束冲突定义引用约束的值不一致在关系中的主体对象和从属对象之间。
当我看到雇主对象时,它试图附加它似乎已经"丢失"了它的Address和PostalAddress项。
这是我的第一个MVC 3项目,所以任何帮助都会很感激。
编辑页面视图如下图
@model MyProject.BusinessObjects.Employer
@{ ViewBag.Title = "Edit Employer Details"; }
<h2>Edit Employer Details</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Employer</legend>
<div class="editor-label">Name</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<fieldset>
<legend>Address</legend>
<div class="editor-label">Line One</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.LineOne)
@Html.ValidationMessageFor(model => model.Address.LineOne)
</div>
<div class="editor-label">Line Two</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.LineTwo)
@Html.ValidationMessageFor(model => model.Address.LineTwo)
</div>
<div class="editor-label">Suburb</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Suburb)
@Html.ValidationMessageFor(model => model.Address.Suburb)
</div>
<div class="editor-label">State</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.State)
@Html.ValidationMessageFor(model => model.Address.State)
</div>
<div class="editor-label">Post Code</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.PostCode)
@Html.ValidationMessageFor(model => model.Address.PostCode)
</div>
</fieldset>
<p>
<input type="submit" value="Save Changes" />
</p>
</fieldset>
}
你能把你的剃刀文件贴出来吗?我猜是模型绑定没有正常工作。你是如何创建表单的?
我建议你使用'ViewModel'模式来解决这个问题。
创建一个包含显示给用户的属性的类,并在操作中获取该对象并将其"转换"到模型对象上。
应该是这样的:
假设你有这些类:
public class Employee{
public string Name {get; set;}
public Address Address {get; set;}
}
public class Address {
public string Street {get; set;}
}
你的视图模型应该是:
public class EmployeeViewModel{
public string Name {get; set;}
public string Street {get; set;}
}
您可以使用Automapper轻松地在这些对象之间进行转换
我想你的ModelBinder有问题。
它可能不适合复杂的类型(我猜Address和PostalAddress是雇主的导航属性)。
你可以试试下面的代码。
[HttpPost]
public ActionResult Edit(Employer employer, FormCollection col)
{
if (ModelState.IsValid)
{
var emp = employer;
//populate emp.Address and emp.PostalAddress with values from col
entities.Employers.Attach(emp);
entities.ObjectStateManager.ChangeObjectState(emp, EntityState.Modified);
entities.SaveChanges();
return RedirectToAction("Index");
}
return View(employer);
}