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

Topics: Administration, Core, General
Aug 11, 2015 at 8:33 AM
Hi,
I encounter below issue while deleting multiple records .Below is my error log.

private void DeletePropertiesFromACategory(List<int> toDeletePropertyList, UsedEquipmentProductCategoryPart part)
    {
if (toDeletePropertyList != null && toDeletePropertyList.Count() >= 1)
        {

            foreach (var propertyId in toDeletePropertyList)
            {
                _propertyCategoryService.DeleteProperty(new UsedEquipmentPropertyCategoryRecord()
                {

                    UsedEquipmentProductCategoryPartRecord_Id = part.Record.Id,
                    UsedEquipmentPropertyPartRecord_Id = propertyId,

                });


            }
        }
    }
Above method is the one which calls a service which indeed calls OrchardRepository method to delete records.

NHibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 13, of entity: MyModule.UsedEquipment.Models.UsedEquipmentPropertyCategoryRecord
at NHibernate.Engine.StatefulPersistenceContext.CheckUniqueness(EntityKey key, Object obj) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Engine\StatefulPersistenceContext.cs:line 698
at NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(DeleteEvent event, ISet transientEntities) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultDeleteEventListener.cs:line 72
at NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(DeleteEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Event\Default\DefaultDeleteEventListener.cs:line 31
at NHibernate.Impl.SessionImpl.FireDelete(DeleteEvent event) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:line 2488
at NHibernate.Impl.SessionImpl.Delete(Object obj) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:line 518
at Orchard.Data.Repository1.Delete(T entity) in c:\code\MyProject\src\Orchard\Data\Repository.cs:line 108
at Orchard.Data.Repository
1.Orchard.Data.IRepository<T>.Delete(T entity) in c:\code\MyProject\src\Orchard\Data\Repository.cs:line 45
at MyModule.UsedEquipment.Service.UsedEquipmentPropertyCategoryService.DeleteProperty(UsedEquipmentPropertyCategoryRecord propertyCategory)
at MyModule.UsedEquipment.Drivers.UsedEquipmentProductCategoryPartDriver.DeletePropertiesFromACategory(List1 toDeletePropertyList, UsedEquipmentProductCategoryPart part)
at MyModule.UsedEquipment.Drivers.UsedEquipmentProductCategoryPartDriver.ManipulateProperties(UsedEquipmentProductCategoryPart part, UsedEquipmentProductCategoryViewModel viewModel)
at MyModule.UsedEquipment.Drivers.UsedEquipmentProductCategoryPartDriver.Editor(UsedEquipmentProductCategoryPart part, IUpdateModel updater, Object shapeHelper)
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at Orchard.ContentManagement.Drivers.ContentPartDriver
1.Orchard.ContentManagement.Drivers.IContentPartDriver.UpdateEditor(UpdateEditorContext context) in c:\code\MyProject\src\Orchard\ContentManagement\Drivers\ContentPartDriver.cs:line 81
at Orchard.ContentManagement.Drivers.Coordinators.ContentPartDriverCoordinator.<>c__DisplayClass10.<UpdateEditor>b__f(IContentPartDriver driver) in c:\code\MyProject\src\Orchard\ContentManagement\Drivers\Coordinators\ContentPartDriverCoordinator.cs:line 63
at Orchard.InvokeExtensions.Invoke[TEvents](IEnumerable1 events, Action1 dispatch, ILogger logger) in c:\code\MyProject\src\Orchard\InvokeExtensions.cs:line 17

How can we overcome this issue.