2011-12-01 14 views
5

Ben Fluent nHibernate için yeni ve bilmek istiyorum, iki sınıf Profil ve E-posta bire-çok aşağıdaki gibi eşlenmiş varsa ... Profil silindi, e-posta kalacak şekilde bir nHibernate haritalama akıcı bir şekilde tanımlamak istiyorum DB'de, bir anahtar Null olarak ayarlanmış. Veya başka bir deyişle herhangi bir yardım greately takdirFluent NHibernate'de "Set null" için "cascade delete" seçeneği nasıl ayarlanır?

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

"ON SET NULL SİL" olması!

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

cevap

7

Bu davranışı Fluent NHibernate AFAIK'de otomatik olarak belirleyemezsiniz. AÇIK SİL rağmen/GÜNCELLEME davranışı ÜZERİNE şartname NHibernate, kaskad davranışın belirli seviyeleri ile basamaklı NH/FNH kontrolünü destekleyen tüm veritabanları için ortaktır: Gördüğünüz gibi

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. 

, "SET NULL" biri değil mevcut kaskad davranışları.

Bu durumda yapabileceğiniz en iyi hiç basamaklandırılmamalıdır değil ve bunun yerine ait oldukları Ne profili "Ters" (E-postaları "kontrol" olarak ilişkiyi tanımlamaktır; Profiller değil "kendi" kendi E do -postalar gibi) ve Repository'de mantık yürütmek veya NHibernate Oturumu'na ekleyerek, çocuğun e-postalarının ana Profiline tüm referanslarını kaldıracak, daha sonra Profili silmeden önce tüm çocukları "artık" kayıtları olarak kaydedin.

+0

Ne düşündüğümü .. Çok teşekkür ederim! –

İlgili konular