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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.