3

別のコンテキストで継承されているコンテキストでエンティティ構成を変更/置換することは可能ですか?

例: Framework というソリューションの Data.Access というプロジェクトに Context があります。その OnModelCreating 関数は、次のようにエンティティ構成を追加します。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
   // TestEntityConfiguration is the configuration of an entity named TestEntity in the Framework solution
   modelBuilder.Configurations.Add(new TestEntityConfiguration());

   // multiple other configurations...
}

FrameworkConsumer と呼ばれる別のソリューションには、Framework ソリューションの Data.Access から Context を拡張する Context クラスを持つ Local.Data.Access プロジェクトがあります。その OnModelCreating 関数は次のようになります。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   // Adds all the configurations from the Context in Data.Access in the Framework solution
   base.OnModelCreating(modelBuilder);

   // Other configurations local to the extended context go here...
}

私の質問はこれです。FrameworkConsumer ソリューションの Local.Data.Access プロジェクトで、追加の構成設定または TestEntity の別の構成を追加したい場合、これはどのように達成できますか? 別の構成を追加しようとしましたが、このエンティティ (TestEntity) が既に構成されているというエラーが表示されます。今のところ、構成を追加する私の解決策は、Local.Data.Access Context クラスの Dispose 関数で Database.ExecuteSqlCommand を使用することでした。エレガントではありませんが、機能します。アイデア/アドバイスをいただければ幸いです。

ありがとう

4

1 に答える 1

0

その構成を別の仮想メソッドに入れることができ、変更する必要がある場合はオーバーライドできます。例えば:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
   // multiple other configurations...
   SpecialConfigurations(modelBuilder);    

}

protected virtual void SpecialConfigurations(DbModelBuilder modelBuilder) 
{
   // TestEntityConfiguration is the configuration of an entity named TestEntity in the Framework solution
   modelBuilder.Configurations.Add(new TestEntityConfiguration());

   // multiple other configurations...
}

そして、SpecialConfigurations メソッドをオーバーライドします。

于 2013-03-12T18:02:02.893 に答える