2012-06-21 4 views
5

古いアプリケーションによって作成され、維持されるSQLサーバーデータベースで動作するC#アプリケーションを作成する必要があります。アプリケーションは毎年新しいテーブルを作成し、テーブルの名前には「yearプロパティ」が含まれます。作成するテーブルの数は、ユーザーがアプリケーション内で作成した「セクション」の数によって異なる場合があります。だから、私はCwx_DRyzのようなテーブルを使って作業しなければなりません( "wx"はセクションとなり、 "yz"は年となります)。表のグループの例としては次のようになります。実行時にエンティティマップを別の「不明な」テーブルに変更する

C01_DR07

C01_DR08

C01_DR09

C02_DR08

C02_DR09

C03_DR06

C04_DR12

これらのテーブルはすべて、たとえばクライアントを表すことができます。彼らは異なるセクションと年のクライアントであるが、同じ構造を持つクライアントである。

私の質問は次のとおりです。クライアントエンティティですべてのテーブルを処理し、実行時にマッピングを変更することはできますか?ランタイムの前にテーブルがわからないので、タイトルは「不明」と表示されます。

私が見つけたもっともよく似た質問はEntity Framework map multiple tables to one entityで、答えは "テーブルコンクリート型継承"を使用することですが、私の場合は役に立ちません。

PS:EFバージョン4.3.1およびVS2010

EDIT:テーブルは主キーを持っていない...それらのほとんどは、一意の値(整数または文字列)を持つようにsupossedされた列を持っています。

答えて

4

「コードファースト」を使用する場合は、必要に応じてマッピングを作成できます。これは、作成したマッピングがデータベースと一致する場合、既存のデータベースでも機能します。

コンテキストを作成するたびに、マップする文字列(tablename)を作成できます。

いくつかの「最初のコード」のためのcodesamplesとどのように始めることができ:

DbContext:

public DbSet<YourEntity> YourEntities { get; set; } 
... 

// this is called when the db gets created and does the configuration for you => maybe not needed in your case 
protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    ConfigurationRegistrar configurationRegistrar = modelBuilder.Configurations; 

    new GeneralEntitiesConfiguration(configurationRegistrar); 
} 

GeneralEntitiesConfigurationがどのように見えるヘルパー以外の何物でも、コンフィギュレーションを処理するために使用してクラスイムではありません:

public class GeneralEntitiesConfiguration 
{ 
    public GeneralEntitiesConfiguration(ConfigurationRegistrar configurationRegistrar) 
    { 
     configurationRegistrar.Add(new YourEntityConfiguration()); 
     //and additional configurations for each entity, just to splitt it a bit and have it more read and maintenance able 
    } 
} 

YourEntityConfigurationは、私はこのエンティティのすべての設定を持っているクラスです。

public class YourEntityConfiguration : EntityTypeConfiguration<YourEntity> 
{ 
    public YourEntityConfiguration() 
    { 
     ToTable("WhatEverYouLike"); // here you can do any magic to map this entity to a table, just make sure that your properties are mapped to the correct colums 
     Property(entity => entity.Id).HasColumnName("YouColumnName"); 

     //and here you also have to do the other configurations 
    } 
} 

アプリケーションの起動時(または最初にコンテキストを初期化する前)に、データベースを初期化する必要があります。したがって、データベースをチェックして差異を処理するイニシャライザを使用することができます。 "DropCreateDatabaseAlways"や "DropCreateDatabaseIfModelChanges" =>のようなものがありますが、違いを無視する独自のものを作成する必要があります。私のサンプルでは、​​私は(私は最初の試行のためのsciptsとモデルの変更を処理したかった)モデルが異なるだけで例外をスローを作成しています

//before using the context the first time i'm calling, you can ignore the connection string 
DbContextInitializer.Init(conString); 

public static class DbContextInitializer 
{ 
    public static void Init (string connectionString) 
    { 
     Database.SetInitializer(new CreateDbThrowExceptionIfModelDiffersInitializer<SMDbContext>()); 

     using(var dbContenxt = new MyDbContext(connectionString)) 
     { 
      try 
      { 
       dbContenxt.Database.Initialize(true); 
      } 
      catch(DatabaseModelDiffersException diffException) 
      { 
       // some magic... 
      } 
      catch(Exception ex) 
      { 
       // TODO: log 
       throw; 
      } 
     } 
    } 

    public class CreateDbThrowExceptionIfModelDiffersInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext 
    { 
     public void InitializeDatabase(TContext context) 
     { 
      using (new TransactionScope(TransactionScopeOption.Suppress)) 
      { 
       if (!context.Database.Exists()) 
        context.Database.Create(); 
      } 

      if (!context.Database.CompatibleWithModel(true)) 
      { 
       throw new DatabaseModelDiffersException("Database Model differs!"); 
      } 
     } 

     protected virtual void Seed(TContext context) 
     { 
      // create data if you like 
     } 
    } 

    // just an exception i'm using for later useage 
    public class DatabaseModelDiffersException : Exception 
    { 
     public DatabaseModelDiffersException(string msg) : base(msg) 
     {} 
    } 
} 

はあなたのアイデアは、動的テーブルを扱うことができる持っている願っていますエンティティフレームワークの名前!

+0

すべてのユーザーがすべての「エンティティ」(テーブルのセット)を作成しているわけではないので、マッピングはデータベースと一致する必要はありません。完全に一致することは必須ですか?とにかく、私はそれらのサンプルを感謝します。 – CarlosJ

+0

週末に投稿します! –

+0

ありがとう、ありがとうございます。 – CarlosJ

関連する問題