ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type
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);
ademg
My passion for technology comes from childhood. I grew up playing with console computers but always wanted to know how the game was made and who made it. My first experience with programming languages was Quick Basic. I experimented with Perl and PHP to finally end up as a #dotnet #developer in love with #ruby and #java #Devops enthusiast with over hundred successful projects. I do have a bunch of Microsoft Certificates and might be obsessive-compulsive for clean code.
Updated on June 04, 2022Comments
-
ademg almost 2 years
I am using EISK (Employee Info Starter Kit) to develop an application. My entity diagram looks like this 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.