1
我有這個映射如何刪除在nhibernate中級聯的集合?
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();
}
}
現在我想刪除一個國家。如果你刪除一個國家,所有的卡片都應該被刪除。它應該刪除所有的獎勵。
nhibernateRepo.Load<Country>(countryId);
nhibernateRepo.Delete<Country>(country);
unitOfWork.Commit();
當我做到這一點。我收到以下錯誤。
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:
爲什麼在發生刪除時試圖進行更新?
Hmmmm.It現在可以刪除
Inverse
。我特別補充說,在發佈之前,我得到一個錯誤(不知道是否這是刪除或當我正在編寫代碼更新國家)。他們似乎現在工作,但我沒有在任何一方的任何逆。不確定自己是否需要一個。誰知道是否沒有其他事情發生。這種逆轉總是讓我感動。 – chobo2 2011-12-29 23:16:02是的,它也讓我一直都在,如果文檔更清晰,這將會有所幫助。 – eulerfx 2011-12-29 23:23:04
我只希望你不用擔心它:) – chobo2 2011-12-29 23:41:37