我正在使用EISK(员工信息入门套件)来开发应用程序。我的实体图看起来像这个我尝试通过这段代码更新应用程序表。
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);
}
有人可以告诉我如何修复此错误并更新表格。 当我尝试更新其他表时,同样的错误发生。插件工作正常。 希望不要打扰你。最诚挚的问候。
答案 0 :(得分:7)
因此它已经属于上下文,您应该更新该上下文。 它不能附加到新的背景, 您可以创建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);