There is no ObjectStateEntry with a reference to an object of type in the ObjectStateManager.

asp.net c# entity entity-framework

Question

EISK (Employee Info Starter Kit) is being used by me to create an application. This is how my entity diagram appears.Entity Diagram I'm attempting to use this code to update the application table.

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

The updating procedure at Business Logic is currently

 [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 someone please explain how to update the tables and cure this error? When I attempt to update additional tables, I see the same issue. The insert is functional. I hope I'm not bothering you. Sincere regards.

1
3
6/21/2011 8:28:51 AM

Accepted Answer

It thus already belongs to a context, which you should change. It cannot be linked to a new context, but you may create a new updatedApplication instance and copy all of its properties to it. Then, attach the new entity to the updatedApplication instance.

Also modify

  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);
8
10/18/2013 6:05:44 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow