[英]How to delete a collection with cascading in nhibernate?
I have this mapping 我有这个映射
public class CountryMapping : ClassMap<Country>
{
public CountryMapping()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name).Not.Nullable().NvarcharWithMaxSize();
HasMany(x => x.Cards).Cascade.Delete().Inverse();
}
}
public class CardMapping : ClassMap<Card>
{
public CardMapping()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name).Not.Nullable().NvarcharWithMaxSize(); ;
References(x => x.Country).Not.Nullable();
HasMany(x => x.RewardTiers).Cascade.All();
}
}
Now I want to delete a country. 现在我要删除一个国家。 If you delete a country all the cards should be deleted. 如果删除国家/地区,则所有卡均应删除。 It should delete all the rewards. 它应该删除所有奖励。
nhibernateRepo.Load<Country>(countryId);
nhibernateRepo.Delete<Country>(country);
unitOfWork.Commit();
When I do this. 当我这样做时。 I get the following error. 我收到以下错误。
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:
Why is it trying to do an update when a delete had happened? 为什么在发生删除时尝试进行更新?
It is trying to do an update because it thinks that Card
is the "owner" of the Country->Cards association and so when a Country is deleted, NH thinks that it needs to null Country references. 它试图进行更新,因为它认为Card
是Country-> Cards关联的“所有者”,因此,删除Country时,NH认为需要将Country引用设为空。 This is caused by the Inverse mapping modifier. 这是由“逆映射”修改器引起的。 According to the documentation for Inverse
: 根据Inverse
文档:
Inverse the ownership of this entity. 与此实体的所有权相反。 Make the other side of the relationship responsible for saving. 使关系的另一端负责保存。
Try removing Inverse
from HasMany(x => x.Cards).Cascade.Delete().Inverse();
尝试从HasMany(x => x.Cards).Cascade.Delete().Inverse();
删除Inverse
HasMany(x => x.Cards).Cascade.Delete().Inverse();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.