我想从产品中删除ICollection Pricebreaks。
我使用以下方法。但是他们似乎没有删除。我错过了什么。
当我跨过。我注意到“product.pricebreaks.clear();”实际上并没有清除项目。我需要冲水还是什么?
公共void removepricebreak(int productid)
{
using (ISession session = EStore.Domain.Helpers.NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
var product = session.Get<Product>(productId);
// i can see 20 PriceBreak records, tho nh prof saids PriceBreak table hasn't been hit
product.PriceBreaks.Clear();
session.SaveOrUpdate(product);
transaction.Commit(); // PriceBreak table is hit here with select statement, no delete?
}
}
这是我的HBM档案
<class name="Product" table="Products">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="CompanyId" column="CompanyId" type="Int32" not-null="true" />
<property name="Name" column="Name"/>
<set name="PriceBreaks" table="PriceBreaks" generic="true" cascade="all-delete-orphan" inverse="true" >
<key column="ProductId" />
<one-to-many class="EStore.Domain.Model.PriceBreak, EStore.Domain" />
</set>
</class>
<class name="PriceBreak" table="PriceBreaks">
<id name="Id" type="Int32" column="Id" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="Product" column="ProductId" not-null="true" class="EStore.Domain.Model.Product, EStore.Domain" />
</class>
我的实体
public class Product
{
public virtual int Id { get; set; }
public virtual ICollection<PriceBreak> PriceBreaks { get; set; }
public virtual void AddPriceBreak(PriceBreak priceBreak)
{
priceBreak.Product = this;
PriceBreaks.Add(priceBreak);
}
}
public class PriceBreak
{
public virtual int Id { get; set; }
public virtual Product Product { get; set; }
}
这是NHPROF的SQL
/ /
选择product0_u.id作为id0_0_
来自产品产品0_
其中,product0_.id=23/*@p0*/
然后
选择pricebreak0_.productid作为productid1_u,
pricebreak0_.id为id1_uuu,
pricebreak0_.id作为id1_0_,
pricebreak0_.productid作为productid1_0_,
从价格突破价格突破0_
其中pricebreak0_.productid=23/*@p0*/
没有更新或选择