I have a bidirectional many-to-many mapping with index column. When I try to delete any child which is not the last one in the collection, deletion fails. Any input is welcome and Thanks a lot in advance.
Mapping file at parent site:
Code:
<class name="EBSimple.Commerce.DomainModel.Category, EBSimple.Commerce.DomainModel" table="EBS_C_Category">
<list name="Products"
table="EBS_C_Category_Product"
lazy="false"
cascade="save-update">
<key column="CategoryId"/>
<index column="DisplayOrder"/>
<many-to-many class="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" column="ProductId"/>
</list>
</class>
Mapping file at child site:
Code:
<class name="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" table="EBS_C_Product">
<bag name="Categories"
table="EBS_C_Category_Product"
inverse="true">
<key column="ProductId"/>
<many-to-many class="EBSimple.Commerce.DomainModel.Category, EBSimple.Commerce.DomainModel" column="CategoryId"/>
</bag>
</class>
Table Schema:
Code:
CREATE TABLE [dbo].[EBS_C_Category_Product] (
[CategoryId] [uniqueidentifier] NOT NULL ,
[ProductId] [uniqueidentifier] NOT NULL ,
[DisplayOrder] [int] NOT NULL
)
Code used to delete a child:
Code:
objCategory.Products.Remove(objProduct);
objProduct.Categories.Remove(objCategory);
SessionManager.GetSession().SaveOrUpdate(objCategory);
Code:
Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
The statement has been terminated.
Source Error:
Line 173: CheckReaders();
Line 174: Prepare(cmd);
Line 175: return cmd.ExecuteNonQuery();
Line 176: }
Line 177:
Source File: C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs Line: 175
Stack Trace:
[SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857386
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734998
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +886
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +415
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs:175
NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\NonBatchingBatcher.cs:36
NHibernate.Persister.Collection.BasicCollectionPersister.DoUpdateRows(Object id, IPersistentCollection collection, ISessionImplementor session) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Persister\Collection\BasicCollectionPersister.cs:222
[ADOException: could not update collection rows: [EBSimple.Commerce.DomainModel.Category.Products#2877d2b2-e890-48dd-8908-98ac0183c17e]]
EBSimple.Core.NHibernateSessionManager.CommitTransaction() in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\Core\NHibernateSessionManager.cs:131
EBSimple.Commerce.HttpModules.NHibernateSessionModule.CommitAndCloseSession(Object sender, EventArgs e) in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\HttpModules\NHibernateSessionModule.cs:43
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
Frank