实体框架持久性问题给出“ObjectStateManager 不包含引用类型对象的 ObjectStateEntry..”

发布于 2024-09-13 01:12:14 字数 6691 浏览 4 评论 0原文

在我的数据库中:

PK CountryID int notnull
PK ServiceID int notnull
PK TaskID int notnull
PK TaskItemID int notnull
PK CorrespondentID int notnull
PK PreviousTask int notnull
   IsOnTimeline bit not null
   ..and 5 other nullable fields

我正在使用这个组合键来确保“ServiceSchedule”项的唯一性。 “Previoustask”字段是所有 PK 的串联,其想法是我可以构建从记录到记录相关的细粒度“ServiceSchedule”任务时间线。

上下文是:

UPEntities db = new UPEntities(); for all workings here (i.e. repository and db).

两个编辑控制器操作如下所示:

     public ActionResult Edit(int countryid,
                                int serviceid,
                                int taskid,
                                int taskitemid,
                                int correspondentid)
    {
        var t = (from s in repository.GetServiceSchedules()
                            where s.CountryID == countryid
                            where s.ServiceID == serviceid
                            where s.TaskID == taskid
                            where s.TaskItemID == taskitemid
                            where s.CorrespondentID == correspondentid
                            select s).First();

        return View(t);
    }



    [HttpPost]
    public ActionResult Edit(ServiceSchedule serviceToEdit)
    {

        var originalService = (from s in repository.GetServiceSchedules()
                               where s.CountryID == serviceToEdit.CountryID
                               where s.ServiceID == serviceToEdit.ServiceID
                               where s.TaskID == serviceToEdit.TaskID
                               where s.TaskItemID == serviceToEdit.TaskItemID
                               where s.CorrespondentID == serviceToEdit.CorrespondentID
                               select s).First();


       if (!ModelState.IsValid)

         return View(originalService);

         db.ApplyPropertyChanges(originalService.EntityKey.EntitySetName, serviceToEdit);        //Code throws exception here.......
         db.SaveChanges();

         return RedirectToAction("Index");           
    }

直到“db.ApplyPropertyChanges”行,一切似乎都工作正常,但我收到错误:

The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'ProjectName.Models.ServiceSchedule'.

我注意到传递到编辑视图的模型包含来自相关表(例如,CountryID 中的国家/地区集合),但当模型从视图返回时,它仅包含“ServciceSchedule”表中的 PK ID,而不包含相关集合。

我目前正在尝试通过在这样的视图中创建hiddenfors来解决这个问题,但似乎仍然无法将完整的模型返回到控制器中:

<% using (Html.BeginForm())
   {%>
    <%= Html.ValidationSummary(true)%>

    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            <%= Html.HiddenFor(model => model.CountryID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.ServiceID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskItemID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.CorrespondentID)%>
        </div>



         <div class="editor-label">
            <%= Html.HiddenFor(model => model.Services)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.Tasks)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskItems)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.Correspondents)%>
        </div>


        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.PreviousTask)%>
            <%= Html.ValidationMessageFor(model => model.PreviousTask)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Cost)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Cost, String.Format("{0:F}", Model.Cost))%>
            <%= Html.ValidationMessageFor(model => model.Cost)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.ChargeOut)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.ChargeOut, String.Format("{0:F}", Model.ChargeOut))%>
            <%= Html.ValidationMessageFor(model => model.ChargeOut)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.DurationInDays)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.DurationInDays, String.Format("{0:F}", Model.DurationInDays))%>
            <%= Html.ValidationMessageFor(model => model.DurationInDays)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.JobSection)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.JobSection)%>
            <%= Html.ValidationMessageFor(model => model.JobSection)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.JobSectionGroup)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.JobSectionGroup)%>
            <%= Html.ValidationMessageFor(model => model.JobSectionGroup)%>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

此外,GetServiceSchedules存储库看起来像这样:(

public class ServicesRepository
{
    UPEntities db = new UPEntities();


    public List<ServiceSchedule> GetServiceSchedules()
    {
        return db.ServiceScheduleSet.Include("Countries")
                                    .Include("Services")
                                    .Include("Tasks")
                                    .Include("TaskItems")
                                    .Include("Correspondents")
                                    .ToList();
    }

}

使用VS 2008,SQL 2008,MVC2)

任何人都可以让我知道一些更多建议的补救措施?

In the database I have have:

PK CountryID int notnull
PK ServiceID int notnull
PK TaskID int notnull
PK TaskItemID int notnull
PK CorrespondentID int notnull
PK PreviousTask int notnull
   IsOnTimeline bit not null
   ..and 5 other nullable fields

I am using this composite key to ensure uniqueness of the "ServiceSchedule" item. The "Previoustask" field being a concatenation of all the PKs, the idea being that I can build a granular "ServiceSchedule" tasks timeline related from record to record.

The context is:

UPEntities db = new UPEntities(); for all workings here (i.e. repository and db).

The two Edit controller actions look like this:

     public ActionResult Edit(int countryid,
                                int serviceid,
                                int taskid,
                                int taskitemid,
                                int correspondentid)
    {
        var t = (from s in repository.GetServiceSchedules()
                            where s.CountryID == countryid
                            where s.ServiceID == serviceid
                            where s.TaskID == taskid
                            where s.TaskItemID == taskitemid
                            where s.CorrespondentID == correspondentid
                            select s).First();

        return View(t);
    }



    [HttpPost]
    public ActionResult Edit(ServiceSchedule serviceToEdit)
    {

        var originalService = (from s in repository.GetServiceSchedules()
                               where s.CountryID == serviceToEdit.CountryID
                               where s.ServiceID == serviceToEdit.ServiceID
                               where s.TaskID == serviceToEdit.TaskID
                               where s.TaskItemID == serviceToEdit.TaskItemID
                               where s.CorrespondentID == serviceToEdit.CorrespondentID
                               select s).First();


       if (!ModelState.IsValid)

         return View(originalService);

         db.ApplyPropertyChanges(originalService.EntityKey.EntitySetName, serviceToEdit);        //Code throws exception here.......
         db.SaveChanges();

         return RedirectToAction("Index");           
    }

Everything seems to work OK up to the "db.ApplyPropertyChanges" line and there I get the error:

The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'ProjectName.Models.ServiceSchedule'.

I have noticed that the model passed into the Edit View contains the collections from the related table (e.g. the countries collection off the CountryID) but when the model returns from the view it contains only the PK IDs from the "ServciceSchedule" table and no related collections.

I am currently trying to counter this problem by creating hiddenfors in the view like this but still cant seem to get the complete model back into the controller:

<% using (Html.BeginForm())
   {%>
    <%= Html.ValidationSummary(true)%>

    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            <%= Html.HiddenFor(model => model.CountryID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.ServiceID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskItemID)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.CorrespondentID)%>
        </div>



         <div class="editor-label">
            <%= Html.HiddenFor(model => model.Services)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.Tasks)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.TaskItems)%>
        </div>

        <div class="editor-label">
            <%= Html.HiddenFor(model => model.Correspondents)%>
        </div>


        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.PreviousTask)%>
            <%= Html.ValidationMessageFor(model => model.PreviousTask)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Cost)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Cost, String.Format("{0:F}", Model.Cost))%>
            <%= Html.ValidationMessageFor(model => model.Cost)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.ChargeOut)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.ChargeOut, String.Format("{0:F}", Model.ChargeOut))%>
            <%= Html.ValidationMessageFor(model => model.ChargeOut)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.DurationInDays)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.DurationInDays, String.Format("{0:F}", Model.DurationInDays))%>
            <%= Html.ValidationMessageFor(model => model.DurationInDays)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.JobSection)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.JobSection)%>
            <%= Html.ValidationMessageFor(model => model.JobSection)%>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.JobSectionGroup)%>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.JobSectionGroup)%>
            <%= Html.ValidationMessageFor(model => model.JobSectionGroup)%>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

Also the GetServiceSchedules Repository looks like this:

public class ServicesRepository
{
    UPEntities db = new UPEntities();


    public List<ServiceSchedule> GetServiceSchedules()
    {
        return db.ServiceScheduleSet.Include("Countries")
                                    .Include("Services")
                                    .Include("Tasks")
                                    .Include("TaskItems")
                                    .Include("Correspondents")
                                    .ToList();
    }

}

(using VS 2008, SQL 2008, MVC2)

Can anyone let me know some more suggeted remedies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文