Showing posts with label NonUniqueObjectException was caught. Show all posts
Showing posts with label NonUniqueObjectException was caught. Show all posts

Saturday, 17 June 2017

NonUniqueObjectException was caught

NonUniqueObjectException was caught

a different object with the same identifier value was already associated with the session: 345, of entity: Kasani.RackApp.Model.Data.Domain.Employee

In The Below Code, The Above Exception Is Coming.

The Exception Came Because The Two Different Objects Having Same Id, Which The NHibernate Reads The Id To SaveOrUpdate The Data Accordingly Based On Id.

So To Resolve That, I Had Used

Session.Clear() and
Session.Flush() Before SaveOrUpdate() method...

Now It Works Fine.

public bool SaveEmployee(EmployeeInformation employeeInformation)
        {
         
                bool isEmployeeSaved = false;
         
                EmployeeTypeRepository employeeTypeRepository = new employeeTypeRepository();
                CompareDomainWithBusinessObjRepository cmpObjRepository = new CompareDomainWithBusinessObjRepository();
                List<AppDomain.Compare> cmpSaveEmp = cmpObjRepository.CompareNotifications(employeeInformation, (int)AppEnumeration.Notifications.Employee, employeeInformation.EmployeeId);
             
                var map = new AutoMap<EmployeeInformation, AppDomain.Employee>();
                AppDomain.Employee employee = map.Create(employeeInformation);
                employee.EmployeeType = employeeTypeRepository.FindById((int)employeeInformation.EmployeeTypeId);
           
             
                Session.Clear();
                Session.Flush();
                employee = SaveOrUpdate(employee);
           
                if (employee.EmployeeId > 0)
                {
                    isEmployeeSaved = true;
                }
               foreach (var item in cmpSaveEmp)
                    {
                        cmpObjRepository.SaveOrUpdate(item);
                    }
                return isEmployeeSaved;
       
   
        }

Saturday, 12 December 2015

NonUniqueObjectException was caught - a different object with the same identifier value was already associated with the session: 345, of entity: Kasani.RackApp.Model.Data.Domain.Employee

NonUniqueObjectException was caught            (NHibernate Exception)

a different object with the same identifier value was already associated with the session: 345, of entity: Kasani.RackApp.Model.Data.Domain.Employee


The Exception Came Because The Two Different Objects Having Same Id, Which The NHibernate Reads The Id To SaveOrUpdate The Data Accordingly Based On Id.

So To Resolve That, I Had Used

Session.Clear() and
Session.Flush() Before SaveOrUpdate() method...i.e.,

Session.Clear();
Session.Flush();
object1=SaveOrUpdate(object1);

foreach(var item in object2)
{
   otherObjectRepository.SaveOrUpdate(item);
}


Now It Works Fine.