MVC 3与实体框架和编辑多个对象导致"参照完整性约束违规"多个、实体、完整性、框架

2023-09-03 04:17:34 作者:软妹.

我有一个MVC 3项目,我的工作使用实体框架作为我的模型。我有一个对象雇主,有地址和的PostalAddress,我想显示(视图)和更新(编辑),在同一时间(即在与雇主的详细信息和地址的详细信息页面进行更新,同时)

我的观点似乎做工精细:

  VAR雇主=(从E在entities.Employers.Include(地址)。包括(的PostalAddress)
                其中,e.EmployerNumber == employerNumber
                选择E)。首先();
返回查看(雇主);
 

我的编辑显示的罚款(即所有的文本框都填充了雇主和详细地址)

  [HttpPost]
公众的ActionResult编辑(雇主的雇主)
{
    如果(ModelState.IsValid)
    {
        entities.Employers.Attach(雇主);
        entities.ObjectStateManager.ChangeObjectState(雇主,EntityState.Modified);
        entities.SaveChanges();
        返回RedirectToAction(指数);
    }
    返回查看(雇主);
}
 

但是,当我去拯救我上entities.Employers.Attach(雇主)线以下异常:

  ASP.NET MVC 玩转五

一个参照完整性约束冲突发生了:该物业   定义参照约束值不相符   之间的本金和依赖对象的关系。

当我看到雇主对象是试图附加它似乎已经丢失它的地址和的PostalAddress的项目。

这是我的第一个MVC 3的项目,所以任何帮助,将AP preciated。

编辑页面视图看起来像这样

  @model MyProject.BusinessObjects.Employer
@ {ViewBag.Title =编辑业主详细资料; }

< H2>编辑业主详细资料及LT; / H>

@using(Html.BeginForm())
{
    @ Html.ValidationSummary(真)
    <字段集>

        <传奇>用人单位与LT; /传说>
            < D​​IV CLASS =编辑标记>名称< / DIV>
            < D​​IV CLASS =主编场>
                @ Html.EditorFor(型号=> model.Name)
                @ Html.ValidationMessageFor(型号=> model.Name)
            < / DIV>

            <字段集>
                <传奇>地址< /传说>
                    < D​​IV CLASS =编辑标记>一号线< / DIV>
                    < D​​IV CLASS =主编场>
                        @ Html.EditorFor(型号=> model.Address.LineOne)
                        @ Html.ValidationMessageFor(型号=> model.Address.LineOne)
                    < / DIV>

                    < D​​IV CLASS =编辑标记>线路二:LT; / DIV>
                    < D​​IV CLASS =主编场>
                        @ Html.EditorFor(型号=> model.Address.LineTwo)
                        @ Html.ValidationMessageFor(型号=> model.Address.LineTwo)
                    < / DIV>

                    < D​​IV CLASS =编辑标记>市郊< / DIV>
                    < D​​IV CLASS =主编场>
                        @ Html.EditorFor(型号=> model.Address.Suburb)
                        @ Html.ValidationMessageFor(型号=> model.Address.Suburb)
                    < / DIV>

                    < D​​IV CLASS =编辑标签→状态< / DIV>
                    < D​​IV CLASS =主编场>
                        @ Html.EditorFor(型号=> model.Address.State)
                        @ Html.ValidationMessageFor(型号=> model.Address.State)
                    < / DIV>

                    < D​​IV CLASS =编辑标记>后code< / DIV>
                    < D​​IV CLASS =主编场>
                        @ Html.EditorFor(型号=> model.Address.Post code)
                        @ Html.ValidationMessageFor(型号=> model.Address.Post code)
                    < / DIV>
            < /字段集>

        &其中p为H.;
            <输入类型=提交值=保存更改/>
        &所述; / P>
    < /字段集>
}
 

解决方案

我认为你是有问题的ModelBinder的。

它可能无法正常工作与复杂的类型,你(我猜地址和的PostalAddress是雇主的导航性能)。

您可以尝试下面的code。

  [HttpPost]
公众的ActionResult编辑(雇主的雇主的FormCollection COL)
{
    如果(ModelState.IsValid)
    {
        VAR EMP =雇主;
        //填充emp.Address和emp.PostalAddress从山坳值
        entities.Employers.Attach(EMP);
        entities.ObjectStateManager.ChangeObjectState(EMP,EntityState.Modified);
        entities.SaveChanges();
        返回RedirectToAction(指数);
    }
    返回查看(雇主);
}
 

I have an MVC 3 project that I am working on using Entity Framework as my model. I have an object "Employer" that has "Address" and "PostalAddress" that I would like to display (view) and update (edit) at the same time (i.e. on page with employer details and address details being updated at the same time)

My view seems to work fine:

var employer = (from e in entities.Employers.Include("Address").Include("PostalAddress")
                where e.EmployerNumber == employerNumber
                select e).First();    
return View(employer);

My edit shows up fine (i.e. all of the textboxes are populated with both employer and address details)

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

But when I go to save I get the following exception on the entities.Employers.Attach(employer) line:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

When I look at the employer object it is trying to attach it seems to have "lost" it's Address and PostalAddress items.

This is my first MVC 3 project so any help would be appreciated.

The edit page view looks like this

@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>
}

解决方案

I think you are having problems with the ModelBinder.

Its probably not working with complex types for you (I'm guessing Address and PostalAddress are navigational properties of Employer).

you could try the code below.

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

 
精彩推荐
图片推荐