2011-12-01 26 views
5

Tôi mới dùng Fluent nHibernate và muốn biết, nếu tôi có hai lớp Profile và Email được ánh xạ từ một đến nhiều như sau ... Tôi muốn xác định bản đồ nHibernate lưu loát để khi Hồ sơ bị xóa, Email sẽ vẫn còn trong DB, với một bộ khóa để Null. Hoặc nói cách khác là có "BẬT DELETE SET NULL"Làm thế nào để thiết lập "cascade xóa" tùy chọn để "Set Null" trong Fluent NHibernate?

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

Mọi trợ giúp đều được đánh giá cao!

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); 
     } 
    } 

Trả lời

7

Bạn sẽ không thể chỉ định hành vi này tự động trong Fluent NHibernate AFAIK. Mặc dù ON DELETE/ON hành vi Cập nhật: đặc điểm kỹ thuật chung cho tất cả DBS rằng NHibernate hỗ trợ, kiểm soát NH/FNH tầng với các mức cụ thể về hành vi thác:

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. 

Như bạn có thể thấy, "SET NULL" không phải là một trong những các hành vi tầng có sẵn.

Điều tốt nhất bạn có thể làm trong trường hợp này là không xếp tầng và thay vào đó xác định mối quan hệ là "Đảo ngược" (E-mail "kiểm soát" cấu hình của chúng; Tiểu sử không "sở hữu" -mails như vậy), và để thực hiện logic hoặc trong kho lưu trữ của bạn hoặc đính kèm vào phiên NHibernate sẽ loại bỏ tất cả các tài liệu tham khảo của email con đến hồ sơ cha mẹ của họ, sau đó lưu tất cả các trẻ em là "mồ côi" hồ sơ trước khi xóa hồ sơ.

+0

Đó là những gì tôi nghĩ .. Cảm ơn bạn rất nhiều! –

Các vấn đề liên quan