开发者

NHibernate cascade delete from another entity collection

I have these classes:

public class User
{
  public IList<Order> LastOrders { get; set;}
}

public class Order {}

Where LastOrders is many-to-many map.

How do I tell (Fluent) NHibernate to remove Order from LastOrders collections for all users when I delete an Order? Is it possible?

That is (db save/load code skipped)

user.LastOrders.Add(order);
Ses开发者_StackOverflow中文版sion.Delete(order);
Assert(!user.LastOrders.Contains(order));

Currently I do it manually (lookup for users, update collection, save) before deletion. Without this, NHibernate can't delete Order because it is referenced by users' LastOrders.


You can safely delete the Order if the collection mapping is set to ignore missing rows.

This will leave orphaned rows in the collection table which will be ignored by NHibernate. These can be cleaned up in some batch process.

HasManyToMany(x => x.LastOrders)
    .NotFound.Ignore();

This will give you faster deletes then your current approach. The disadvantage is that your collection tables will be inconsistent with your model for a time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜