1

次のような状況があります。

db エンティティを持つAssemblyXは、外部のassemblyYを参照し、そのクラスを基本クラスとして使用します。 AssemblyXは、必要に応じていくつかのナビゲーション プロパティを追加します。

もちろん、このすべてのエンティティをコピーして automapper を使用して問題を解決できますが、既存のエンティティを継承する理由があります。

したがって、 assemblyY にFoo {}クラスがあり、assemblyXに作成Bar: Foo {}ます。流暢な自動マッピングを無視するように構成しFoo (.IgnoreBase<Foo>())ます。すべてがうまくいきます。しかし、assemblyYには、 を参照するナビゲーション プロパティを持つ別のエンティティRab(流暢な自動マッピングでカバーされる) がありますFoo。これにより、「テーブル Rab からの関連付けは、マップされていないクラスを参照しています: Foo」という例外が発生します。

public class Foo // .IgnoreBase<Foo>() - I require only derived Bar in DB
{
    public virtual Guid Id {get; set;}        
}

public class Bar: Foo
{
    public virtual Guid Id {get; set;}
    // Some new properties
}

public class Rab
{
    public virtual Guid Id {get; set;}
    public virtual Foo Foo // reference to unmapped class, I could not change type to Bar (external assembly)
}

どうすれば解決できますか。Rab を継承しnew Fooて Bar 型でプロパティを作成しようとしましたが、うまくいきませんでした。

アドバイスをよろしくお願いします。

4

1 に答える 1

0

カスタム IAutomappingConfiguration を使用して、マップする必要がある型またはメンバーを決定できます。

class MyConfig : DefaultAutomappingConfiguration
        {
            public override bool ShouldMap(Type type)
            {
                return  !(typeof(Foo)).IsAssignableFrom(type);
            }

            public override bool ShouldMap(Member member)
            {
                return  !(typeof(Foo)).IsAssignableFrom(member.PropertyType);                }
        }

次に、次のように構成を使用できます。

AutoPersistenceModel model = new AutoPersistenceModel(new MyConfig());

そして最後に :

Fluently.Configure()

                    .Mappings(m => m.UsePersistenceModel(model))
                    .BuildSessionFactory();
于 2014-06-24T11:18:19.897 に答える