2011-12-01 17 views
5

私はFluent nHibernateを初めて使用しています。プロファイルと電子メールのマッピングが1対多の場合は、次のようにします。プロファイルが削除されても電子メールが残るようにnHibernateマッピングを流暢に定義したいDBで、キーをNullに設定します。または他の言葉で "DELETE SET NULLのON"流行NHibernateで "カスケード削除"オプションを "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 

を持っているすべてのヘルプはgreately歓迎です!

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

答えて

7

あなたは流暢NHibernateは私の知る限りでは、自動的にこの動作を指定することはできません。 ON DELETEをけれども/ UPDATE挙動に関する仕様はNHibernateのは、カスケード行動の特定のレベルでカスケードNH/FNH制御サポートするすべてのDBに共通している:あなたが見ることができるように

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が」の一つではありません利用可能なカスケード動作。

この場合、できるだけカスケードするのではなく、 "Inverse"(電子メールは自分がどのプロファイルに属しているかを制御します;プロファイルは自分のEを "所有"しません)あなたのリポジトリにロジックを実装するか、またはHibernate Sessionにアタッチして、チャイルドEmailの参照を親プロファイルにすべて削除し、プロファイルを削除する前にすべての子を「孤立した」レコードとして保存します。

+0

私が思ったことは..非常にありがとう! –

関連する問題