0

NHibernate Automapperを使用して、次のクラスをマップしたいと思います。

public class AdminUser: Identity, IIdentityWithRoles 
{
   public virtual IList<Role> Roles { get; set; }         
}

問題は、Automapperが、ロールにadminuserIdがあるスキーマ内に多対1の関係を作成することです。しかし、私はこれで多対多になる役割が必要です。注Roleクラスを変更してIListを含めることはできません。これは、別のライブラリにあり、AdminUserの知識がないためです。

私が本当にやりたいのは、このように属性を追加できるようにすることです。

public class AdminUser: Identity, IIdentityWithRoles 
{
   [ManyToMany]
   public virtual IList<Role> Roles { get; set; }         
}

これにより、automapperはこれを強制的に実行します。Automapper構成を調整してこの属性を探すことは可能ですか、それともすでにこの仕事をしているFluentNhibernateに組み込まれているものがありますか?

あなたが提供できるどんな助けにも感謝します。

アップデート -

ポインタ(賛成)に感謝しますが、今はここで立ち往生しています:

public class ManyToManyConvention : AttributePropertyConvention<ManyToManyAttribute>

    {
        protected override void Apply(ManyToManyAttribute attribute, IPropertyInstance instance)
        {
            How can I now say that this property should be a many to may relationship
        }
    }

    public class ManyToManyAttribute : Attribute
    {
    }
4

1 に答える 1

3

AdminUserデフォルトのマッピングのオーバーライドを作成する必要があります。

public class AdminUserOverride : IAutoMappingOverride<AdminUser>
{
  public void Override(AutoMapping<AdminUser> mapping)
  {
    mapping.HasManyToMany(x => x.Roles); // and possibly other options here
  }
}

そしてそれをあなたの:に登録してAutoMappingください

Fluently.Configure()
  .Database(/* database config */)
  .Mappings(m =>
    m.AutoMappings
      .Add(AutoMap.AssemblyOf<AdminUser>().UseOverridesFromAssemblyOf<AdminUser>()))

これがこの1つの場所よりも多く発生する場合は、に置き換えることができますがConventionこれはもう少し複雑です。

于 2011-04-06T17:18:49.580 に答える