NHibernate crashes when being called from a web user control in ASP.Net -


I have a very strange problem: the NHibernate crashes when called from the web user control.

I am working on an ASP.net web page which uses NHibernate to access a database.

And I have a simple factory class, which is entering the current column in the table ProjectInfo:

  Public class ProjectEntity {Personal Int? _currentStepNumber; Public Virtual Int? Get the current step number {Get {return _currentStepNumber; } Set {_currentStepNumber = value; }} Public static ProjectWizardEntity GetById (int id, bool shouldLock) {return repository redirect.product wizardRepository.GetById (id, shouldLock); } Public stable ProjectWizardEntity GetById (int id) {GetById (ID, false) return; } Public Virtual Zero Save () {Repository Factor.Project wizardRepository.Save (this); } Public Virtual Zero SaveOrUpdate () {Repositirefinite. Project wizardRepository.SaveOrUpdate (this); }}  

This class is delivered through a proxy class, so that every time a new value is assigned, it is flown to the database:

  public class DBHelper {ProjectEntity _projectEntity; ProjectEntity GetProjectEntity () {if (_projectEntity == empty) // get _projectEntity = // or create a new one; Return _projectEntity; } Public int? Get current step {{return (current project> gt; 0)? CurrentProjectWizardEntity.CurrentStepNumber: 0; } Set {if (CurrentProjectId> 0) {CurrentProjectWizardEntity.CurrentStepNumber = Value; CurrentProjectWizardEntity.SaveOrUpdate (); Problem: 

  1. When I use current stop number from the test.aspx page, So everything works properly
  2. When I read this user with the web user control (test.ascx) which is used on the test.aspx page, it is still okay
  3. However when I try to specify a value for the current step number in the code (test.ascx) behind the control: let me The one exception is found:

NHibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session

In reality, the SaveOrUpdate method of the NHibernate repository throws an exception.

I could not understand what the problem might be, so any help would be appreciated.

I think you have a session management problem, how to create a repositoryfinic.product wizard repository.getbid and Probably disposing of the NHibernate session? Does it create and then closes a session?

It seems that on DB GateProjectEntiti () is building or loading a project entity. Later, when Setter of the current Stampnumber called me, you inserted or updated the object in the database.

The problem is that when the DBHlper. GetProxyNTT () is loading an existing object and after shutting down the session is shutting down but keep the object all around, we are entering deep water. When the object is updated later, then you set a new value for the current story and send the object to save it from NHibernate. The problem here is that the object is not associated with the new session created and closed during the saving. Nhibernate is confused since a new object is discovered which has not been loaded from the current session but it has an existing object ID.

For the solution, "NHibernate asp.net session management" and you will find many good hits about how to use the Google ASP.NET request cycle as a unit of work.


Comments