Mam dziwne zachowanie z nhibernate. Problem polega na tym, że nhibernate wykonuje aktualizację przed usunięciem encji. Mam klasę kategorii i klasę produktu. Kategoria ma torbę z produktami . Kiedy usunąć produkt z kategorii, NHibernate robi następujący:NHibernate aktualizuje wiersz przed usunięciem?
- To aktualizuje jednostkowego produktu którego usuwany z kolekcji
- Usuwa podmiot produktów z bazy danych.
Tutaj odwzorowania
<class name="Category">
<id name="Id">
<generator class="hilo" />
</id>
<property name="Name" lazy="false" length="20" />
<bag name="Products" cascade="all-delete-orphan" lazy="false"
inverse="false">
<key column="CategoryId" />
<one-to-many class="Product" />
</bag>
</class>
<class name="Product">
<id name="Id">
<generator class="hilo" />
</id>
<property name="Name" lazy="false" />
<property name="Discontinued" lazy="false" />
<property name="Price" lazy="false" />
<many-to-one name="Category"
class="Category"
column="CategoryId"
cascade="none" />
</class>
Oto kod
using (var session = NHibernateHelper.OpenSession())
using (var transaction = session.BeginTransaction())
{
var c1 = session.Load<Category>(32768);
c1.Ps.RemoveAt(0);
session.SaveOrUpdate(c1);
transaction.Commit();
}
i oto wynik:
exec sp_executesql N'UPDATE Product SET CategoryId = null WHERE
CategoryId = @p0 AND Id = @p1',N'@p0 int,@p1 int',@p0=32768,@p1=65537
go
exec sp_executesql N'DELETE FROM Product WHERE Id = @p0',N'@p0
int',@p0=65537
go
Każdy może wyjaśnić to dziwne zachowanie?
Dzięki.
Dzięki, twój wpis był bardzo pomocny :) – Davita