문제

I have two tables:

  • Receipt
  • ReceiptJournal

Receipt has 0...n ReceiptJournals

The mapping for this (in Receipt) looks like:

this.HasMany(x => x.ReceiptJournals).AsSet().Fetch.Select().Inverse().Cascade.Delete();

The underlying database which I use is SQL Server CE 4.0.

Now, when I execute a delete-statement

const string sql = "delete from Receipt where IsFinished = 0 and IsParked = 0";
var query = NHibernateHelper.CurrentSession.CreateSQLQuery(sql);
query.ExecuteUpdate();

I get an exception:

Could not execute native bulk manipulation query:delete from Receipt where IsFinished = 0 and IsParked = 0[SQL: delete from Receipt where IsFinished = 0 and IsParked = 0]

System.Exception {NHibernate.Exceptions.GenericADOException}

[System.Data.SqlServerCe.SqlCeException] {"Der Primärschlüsselwert kann nicht gelöscht werden, da noch Verweise auf diesen Schlüssel vorhanden sind. [ Foreign key constraint name = FK_Receipt_ReceiptJournal ]"} System.Data.SqlServerCe.SqlCeException

Does NHibernate with SQL Server CE not support cascade delete or am I doing something wrong?

도움이 되었습니까?

해결책

You probably want Cascade.AllDeleteOrphan(). It's a timing problem, NHibernate is attempting to delete from Receipt first but can't because of the foreign key constraint. Setting the cascade strategy to AllDeleteOrphan() will cause it to delete the ReceiptJournal records first because they will be orphaned.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top