如何设置“级联删除”选项设置为“设置为空”在流利的NHibernate? [英] How to set "cascade delete" option to "Set Null" in Fluent NHibernate?

查看:124
本文介绍了如何设置“级联删除”选项设置为“设置为空”在流利的NHibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的流利nHibernate,并想知道,如果我有两个类配置文件和电子邮件映射一对多如下...我想定义一个nHibernate映射流利,所以当配置文件被删除,电子邮件将留在数据库中,并将键设置为空。换句话说,要有ON DELETE SET NULL。

  ALTER TABLE [dbo]。[Email] WITH CHECK ADD CONSTRAINT [ FK4239B252F6539048] FOREIGN KEY([ProfileId])
REFERENCES [dbo]。[Profile]([Id])
ON UPDATE SET NULL
ON DELETE SET NULL


$ b $ p
$ b $ pre $ code > public sealed class ProfileMapping:ClassMap< Profile>
{
public ProfileMapping()
{
//其他一些字段在这里...
HasMany(x => x.Emails);
}
}

public class EmailMapping:ClassMap< Email>
{
public EmailMapping()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Address).Not.Nullable()。UniqueKey(UX_EmailAddress)。Length(254);
Map(x => x.Confirmed);



$ div $解析方案

将无法在Fluent NHibernate AFAIK中自动指定此行为。尽管ON DELETE / ON UPDATE行为规范对NHibernate支持的所有数据库都是通用的,但是NH / FNH控制级联特定的级联行为级别:

  none  - 不要做任何级联,让用户自己处理它们。 
save-update - 当对象被保存/更新时,检查这些关联并保存/更新任何需要它的对象(包括保存/更新多对多情景中的关联)。
delete - 当对象被删除时,删除关联中的所有对象。
delete-orphan - 删除对象时,删除关联中的所有对象。除此之外,当一个对象从assoication中删除,而不是与另一个对象(孤儿)assoicated,也删除它。
all - 当一个对象是保存/更新/删除,检查assoications并保存/更新/删除找到的所有对象。
all-delete-orphan - 当一个对象保存/更新/删除时,检查相关的配置并保存/更新/删除所有找到的对象。除此之外,当一个对象被从assoication中删除,而不与另一个对象(孤儿)相关时,也删除它。

正如您所看到的,SET NULL不是可用的级联行为之一。 >

在这种情况下,您所能做的最好的办法是不要级联,而是将关系定义为反向(电子邮件控制它们属于哪个配置文件;个人资料不拥有他们的电子邮件),并实施逻辑或者在您的存储库或附加到NHibernate会议,将删除所有子电子邮件引用他们的父母配置文件,然后保存所有的孩子为孤儿删除配置文件之前的记录。


I am new to Fluent nHibernate and would like to know, if I have two classes Profile and Email mapped one-to-many as following... I want to define a nHibernate mapping fluently so when Profile is deleted, Email will remain in the DB, with a key set to Null. Or in other words to have "ON DELETE SET NULL"

ALTER TABLE [dbo].[Email]  WITH CHECK ADD  CONSTRAINT [FK4239B252F6539048] FOREIGN KEY([ProfileId])
REFERENCES [dbo].[Profile] ([Id])
ON UPDATE SET NULL
ON DELETE SET NULL

Any help is greately appreciated!

public sealed class ProfileMapping : ClassMap<Profile>
        {
            public ProfileMapping()
            { 
                // Some other fields here ...
                HasMany(x => x.Emails);
            }
        }

    public class EmailMapping : ClassMap<Email>
    {
        public EmailMapping()
        {
            Id(x => x.Id).GeneratedBy.GuidComb();
            Map(x => x.Address).Not.Nullable().UniqueKey("UX_EmailAddress").Length(254);
            Map(x => x.Confirmed);
        }
    }

解决方案

You won't be able to specify this behavior automatically in Fluent NHibernate AFAIK. Although the ON DELETE/ON UPDATE behavior specification is common to all DBs that NHibernate supports, NH/FNH control cascading with specific levels of cascade behavior:

none - do not do any cascades, let the users handles them by themselves.
save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
delete - when the object is deleted, delete all the objects in the assoication.
delete-orphan - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
all-delete-orphan - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

As you can see, "SET NULL" is not one of the available cascade behaviors.

The best you can do in this case is to not cascade at all, and instead to define the relationship as "Inverse" (E-mails "control" what profile they belong to; Profiles do not "own" their E-mails as such), and to implement logic either in your Repository or attached to the NHibernate Session that will remove all references of the child Emails to their parent Profile, then save all the children as "orphan" records before deleting the Profile.

这篇关于如何设置“级联删除”选项设置为“设置为空”在流利的NHibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆