ObjectStateManager 不包含具有对 类型对象的引用的 ObjectStateEntry

发布于 2024-11-16 13:11:41 字数 2142 浏览 0 评论 0原文

我正在使用 EISK(员工信息入门套件)来开发应用程序。我的实体图看起来像这样 Entity Diagram 我尝试通过此代码更新应用程序表。

           int apId = Convert.ToInt32(Request.QueryString["ApplicationID"]);

            ApplicationBLL objGetApplication = new ApplicationBLL();

            Appdec.YEP.BusinessEntities.Application objApplication =
            objGetApplication.GetApplicationByApplicationID(apId);



            objApplication.Status = (ddlStatus.SelectedValue == "0" ? false : true);



            new ApplicationBLL(new Appdec.YEP.DataAccessLayer.DatabaseContext()).UpdateApplication(objApplication);

现在业务逻辑的更新方法是

 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
    public void UpdateApplication(Application updatedApplication)
    {
        // Validate Parameters
        if (updatedApplication == null)
            throw (new ArgumentNullException("updatedApplication"));

        // Validate Primary key value
        if (updatedApplication.ApplicationID.IsInvalidKey())
            BusinessLayerHelper.ThrowErrorForInvalidDataKey("ApplicationID");

        // Apply business rules
        OnApplicationSaving(updatedApplication);
        OnApplicationUpdating(updatedApplication);

        //attaching and making ready for parsistance
        if (updatedApplication.EntityState == EntityState.Detached)
            _DatabaseContext.Applications.Attach(updatedApplication);



_DatabaseContext.ObjectStateManager.ChangeObjectState(updatedApplication, System.Data.EntityState.Modified);//this line throws the error 
//ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type
        int numberOfAffectedRows = _DatabaseContext.SaveChanges();
        if (numberOfAffectedRows == 0) 
            throw new DataNotUpdatedException("No application updated!");

        //Apply business workflow
        OnApplicationUpdated(updatedApplication);
        OnApplicationSaved(updatedApplication);

    }

有人可以告诉我如何修复此错误并更新表吗? 当我尝试更新其他表时也会发生同样的错误。插件工作正常。 希望不要打扰您。此致。

I am using EISK (Employee Info Starter Kit) to develop an application. My entity diagram looks like this Entity Diagram I try to update the application table via this code.

           int apId = Convert.ToInt32(Request.QueryString["ApplicationID"]);

            ApplicationBLL objGetApplication = new ApplicationBLL();

            Appdec.YEP.BusinessEntities.Application objApplication =
            objGetApplication.GetApplicationByApplicationID(apId);



            objApplication.Status = (ddlStatus.SelectedValue == "0" ? false : true);



            new ApplicationBLL(new Appdec.YEP.DataAccessLayer.DatabaseContext()).UpdateApplication(objApplication);

now the update method at bussiness logic is

 [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
    public void UpdateApplication(Application updatedApplication)
    {
        // Validate Parameters
        if (updatedApplication == null)
            throw (new ArgumentNullException("updatedApplication"));

        // Validate Primary key value
        if (updatedApplication.ApplicationID.IsInvalidKey())
            BusinessLayerHelper.ThrowErrorForInvalidDataKey("ApplicationID");

        // Apply business rules
        OnApplicationSaving(updatedApplication);
        OnApplicationUpdating(updatedApplication);

        //attaching and making ready for parsistance
        if (updatedApplication.EntityState == EntityState.Detached)
            _DatabaseContext.Applications.Attach(updatedApplication);



_DatabaseContext.ObjectStateManager.ChangeObjectState(updatedApplication, System.Data.EntityState.Modified);//this line throws the error 
//ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type
        int numberOfAffectedRows = _DatabaseContext.SaveChanges();
        if (numberOfAffectedRows == 0) 
            throw new DataNotUpdatedException("No application updated!");

        //Apply business workflow
        OnApplicationUpdated(updatedApplication);
        OnApplicationSaved(updatedApplication);

    }

Can somebody tell me how to fix this error and update the tables.
the same error ocurres when i try to update other tables also. The insert works fine.
Hoping not to bother you. Best Regards.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-11-23 13:11:41

所以它已经属于一个上下文,你应该更新该上下文。
它不能附加到新的上下文中,
您可以创建updatedApplication 的一个新实例,并将updatedApplication 的所有属性复制到该新实例,并将新实体附加到应用程序。

  if (newApp .EntityState == EntityState.Detached)
            _DatabaseContext.Applications.Attach(newApp );

改为

var newApp = Application ();
 //Copy all propery of updatedApplication to  newApp here 

            if (newApp .EntityKey == null || newApp .EntityKey.IsTemporary)
            {
                _DatabaseContext.Applications.AddObject(newApp );
            }
            else
            {
                _DatabaseContext.Applications.Attach(newApp );
            }
_DatabaseContext.ObjectStateManager.ChangeObjectState(newApp , System.Data.EntityState.Modified);

So it already belongs to a context,and you should update that context.
It can't be attached to new context,
You can create a new instance of updatedApplication and copy all properties of updatedApplication to this new one and attach new entity to application.

Also change

  if (newApp .EntityState == EntityState.Detached)
            _DatabaseContext.Applications.Attach(newApp );

to

var newApp = Application ();
 //Copy all propery of updatedApplication to  newApp here 

            if (newApp .EntityKey == null || newApp .EntityKey.IsTemporary)
            {
                _DatabaseContext.Applications.AddObject(newApp );
            }
            else
            {
                _DatabaseContext.Applications.Attach(newApp );
            }
_DatabaseContext.ObjectStateManager.ChangeObjectState(newApp , System.Data.EntityState.Modified);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文