0

私はレガシーコードを使用しています(したがって、変更をできるだけ少なくしたいのですが)、ここでの多対多の関係に少し問題があります。

これが私が持っているものです:

public class Feature {
   List<Profile> Profiles{get;set;}
}

public class Profile{
   List<FeatureProfile> Features{get;set;}
}

public class FeatureProfile {
   Feature Feat {get;set;}
   Profile Profile {get;set;}
}

そしてそれらのマッピングは次のようになります:

mapper.Class<Feature>(m=>
    m.Bag(x => x.Profiles, bagMap =>
                    {
                        bagMap.Table("FeatureProfile");
                        bagMap.Key(key=>key.Column("FeatureId"));
                        bagMap.Cascade(Cascade.All | Cascade.DeleteOrphans);
                    },map=>map.ManyToMany(manyToMany=>manyToMany.Column("ProfileId")))
);


mapper.Class<Profile>(m=>
    m.Bag(x => x.Features, bagMap =>
                    {
                        bagMap.Key(key=>key.Column("ProfileId"));
                        bagMap.Inverse(true);
                        bagMap.Cascade(Cascade.All | Cascade.DeleteOrphans);
                   })
);

mapper.Class<FeatureProfile>(m=> {
    m.ManyToOne(x => x.Profile, x => x.Column("ProfileId"));
    m.ManyToOne(x => x.Feat, x => x.Column("FeatureId"))
});

必要なのは、機能を削除すると、FeatureProfileも削除されるということです。これはおそらくNHibernate2.xで機能したと思うことに注意してください

4

1 に答える 1

1

私の経験はXMLマッピングの方が多いですが、とにかく以下の行が役立つと思います。NHibernateは、m:nリリースの直接マッピングを提供します(上記の例のように、 Pair-tableを使用)。オブジェクトFeatureProfileを完全に削除できます。関係は暗黙的にマッピングされ、両端(プロファイルまたは機能)のいずれかを削除するときにも同じことが適用されます

<class name="Feature"...>
... 
<bag name="Profiles" lazy="true" 
    table="FeatureProfile" cascade="none" >
  <key column="FeatureId" />
  <many-to-many class="Profile" column="ProfileId" />
</bag>
...

<class name="Profile"...>
... 
<bag name="Features" lazy="true" 
    table="FeatureProfile" cascade="none" >
  <key column="ProfileId" />
  <many-to-many class="Feature" column="FeatureId" />
</bag>
...

そしてこの場合、NHibernateには、機能を削除するときに、ペアテーブルレコードも削除する他の方法はありません(DBの一貫性を損なうことはできません/すべきではありません)。

編集済み:この場合、バッグのカスケードはなしである必要があります。delete-orphanは、本当に危険な削除を引き起こします。ペアだけでなく、その関係の終わりも同様です。

OPによる編集:コードによるマッピングを使用すると、次のようになります。

mapper.Class<Profile>(m =>
{
    m.Bag(x => x.Features, bagMap =>
    {
        bagMap.Table("FeatureProfile");
        bagMap.Key(key=>key.Column("ProfileId"));
        bagMap.Lazy(CollectionLazy.Lazy)
        bagMap.Inverse(false);
        bagMap.Cascade(Cascade.None);
    },map=>map.ManyToMany(manyToMany=>manyToMany.Column("FeatureId")))
}

mapper.Class<Feature>(m =>
{
    m.Bag(x => x.Profiles, bagMap =>
    {
        bagMap.Table("FeatureProfile");
        bagMap.Key(key=>key.Column("FeatureId"));
        bagMap.Lazy(CollectionLazy.Lazy)
        bagMap.Inverse(false);
        bagMap.Cascade(Cascade.None);
    },map=>map.ManyToMany(manyToMany=>manyToMany.Column("ProfileId")))
}); 
于 2012-10-06T15:26:11.143 に答える