Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete a collection with cascading in nhibernate?
    text
    copied!<p>I have this mapping</p> <pre><code> public class CountryMapping : ClassMap&lt;Country&gt; { public CountryMapping() { Id(x =&gt; x.Id).GeneratedBy.GuidComb(); Map(x =&gt; x.Name).Not.Nullable().NvarcharWithMaxSize(); HasMany(x =&gt; x.Cards).Cascade.Delete().Inverse(); } } public class CardMapping : ClassMap&lt;Card&gt; { public CardMapping() { Id(x =&gt; x.Id).GeneratedBy.GuidComb(); Map(x =&gt; x.Name).Not.Nullable().NvarcharWithMaxSize(); ; References(x =&gt; x.Country).Not.Nullable(); HasMany(x =&gt; x.RewardTiers).Cascade.All(); } } </code></pre> <p>Now I want to delete a country. If you delete a country all the cards should be deleted. It should delete all the rewards.</p> <pre><code>nhibernateRepo.Load&lt;Country&gt;(countryId); nhibernateRepo.Delete&lt;Country&gt;(country); unitOfWork.Commit(); </code></pre> <p>When I do this. I get the following error.</p> <pre><code>NHibernate.Exceptions.GenericADOException was caught Message=could not delete collection: [Domain.Card.RewardTiers#7abaade7-4653-456f-8840-9fc700fa949b][SQL: UPDATE [RewardTier] SET Card_id = null WHERE Card_id = @p0] Source=NHibernate SqlString=UPDATE [RewardTier] SET Card_id = null WHERE Card_id = @p0 StackTrace: at NHibernate.Persister.Collection.AbstractCollectionPersister.Remove(Object id, ISessionImplementor session) at NHibernate.Action.CollectionRemoveAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() at CCRecomendator.Framework.Data.UnitOfWork.Commit() in UnitOfWork.cs:line 52 at CCRecomendator.Framework.Services.CountryService.DeleteCountry(Guid countryId) in CountryService.cs:line 157 InnerException: System.Data.SqlClient.SqlException Message=Cannot insert the value NULL into column 'Card_id', table 'cc.dbo.RewardTier'; column does not allow nulls. UPDATE fails. The statement has been terminated. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=16 LineNumber=1 Number=515 Procedure="" Server=(local) State=2 StackTrace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.Persister.Collection.AbstractCollectionPersister.Remove(Object id, ISessionImplementor session) InnerException: </code></pre> <p>Why is it trying to do an update when a delete had happened?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload