2011-12-01 22 views
5

Jestem nowy w Fluent nHibernate i chciałbym wiedzieć, czy mam dwie klasy Profil i e-mail zmapowany jeden-do-wielu jako następujące ... Chcę zdefiniować mapowanie nHibernate płynnie, więc po usunięciu profilu, e-mail pozostanie w DB, z kluczem ustawionym na Null. Lub innymi słowy, aby mieć "USUŃ USTAW NULL"Jak ustawić opcję "usuwanie kaskadowe" na "Ustaw zerowe" w płynnym 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 

Każda pomoc jest bardzo ceniona!

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

Odpowiedz

7

Nie można określić tego zachowania automatycznie w Fluent NHibernate AFAIK. Chociaż ON DELETE/ON zachowania UPDATE specyfikacja jest wspólny dla wszystkich DB że NHibernate obsługuje, kontrolę NH/FNH kaskadowe z konkretnymi poziomami zachowaniu kaskadowej:

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. 

Jak widać, „SET NULL” nie jest jednym z dostępne zachowania kaskadowe.

Najlepsze, co można zrobić w tym przypadku, to wcale nie kaskadować, a zamiast tego zdefiniować relację jako "Odwrotną" ("E-maile", "kontrolować", do jakiego profilu należą, Profile nie "posiadają" ich E jako takie) i do implementacji logiki w repozytorium lub dołączonej do sesji NHibernate, która usunie wszystkie odniesienia do wiadomości e-mail dzieci do ich profilu macierzystego, a następnie zapisz wszystkie dzieci jako "osierocone" rekordy przed usunięciem profilu.

+0

To, co myślałem ... Dziękuję bardzo! –

Powiązane problemy