C# NHibernate 级联 = 保存更新”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1152060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:27:57  来源:igfitidea点击:

NHibernate Cascade=save-update"?

c#nhibernatenhibernate-cascade

提问by John

Disclaimer: I am an NHibernate noobie so hopefully this question makes sense. I have a many-to-many relationship between two classes something like…

免责声明:我是一个 NHibernate noobie 所以希望这个问题是有道理的。我在两个类之间有一个多对多的关系,比如……

public class Entity1
{
    public virtual Guid EntityId { get; set; }
    public virtual IList<Entity2> Entity2List;
} 

Public class Entity2
{
    public virtual Guid EntityId { get; set; }
    public virtual IList<Entity1> Entity1List;
}

I've added a many-to-many relationship with a bag in both class mappings, defining an association table but am unsure of which cascade option to use. I want to be able to create a new Entity1 instance, add a new Entity2 instance to it's list, call Save, and both be inserted into the database (and vice-versa). When deleting an entity it should delete any associations to child entities but not the child entity itself. Should I be using cascade="save-update"?

我在两个类映射中都添加了与包的多对多关系,定义了一个关联表,但不确定要使用哪个级联选项。我希望能够创建一个新的 Entity1 实例,将一个新的 Entity2 实例添加到它的列表中,调用 Save,并将两者都插入到数据库中(反之亦然)。删除实体时,它应该删除与子实体的任何关联,但不删除子实体本身。我应该使用级联=“保存更新”吗?

采纳答案by g .

Yes. It sounds like 'save-update' is what you want, in this case.

是的。在这种情况下,听起来“保存更新”就是您想要的。

I never found a great explanation of each cascade option in the documentation, but have used this blog postby Ayende as reference.

我从来没有在文档中找到对每个级联选项的很好的解释,但我使用了 Ayende 的这篇文作为参考。

  • none- do not do any cascades, let the users handles them by themselves.
  • save-update- when the object is saved/updated, check the associations and save/update any object that require it (including save/update the associations in many-to-many scenario).
  • delete- when the object is deleted, delete all the objects in the association.
  • delete-orphan- when the object is deleted, delete all the objects in the association. In addition to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.
  • all- when an object is save/update/delete, check the associations and save/update/delete all the objects found.
  • all-delete-orphan- when an object is save/update/delete, check the associations and save/update/delete all the objects found. In additional to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.
  • none- 不做任何级联,让用户自己处理。
  • save-update- 当对象被保存/更新时,检查关联并保存/更新任何需要它的对象(包括在多对多场景中保存/更新关联)。
  • delete- 当对象被删除时,删除关联中的所有对象。
  • delete-orphan- 当对象被删除时,删除关联中的所有对象。除此之外,当一个对象从关联中删除并且不与另一个对象关联(孤立)时,也将其删除。
  • all- 当一个对象被保存/更新/删除时,检查关联并保存/更新/删除所有找到的对象。
  • all-delete-orphan- 当一个对象被保存/更新/删除时,检查关联并保存/更新/删除所有找到的对象。除此之外,当一个对象从关联中删除并且不与另一个对象关联(孤立的)时,也将其删除。