2011-09-25 6 views
1

私は、データベース内に列構造に関して同じ複数のテーブルを持っています。複数のエンティティをEF4抽象基本クラスまたはインタフェースにマップしますか?

など、私は彼らのすべてを単一の抽象基本クラスから継承されるように、これはEF4でモデル化したいのですが、私は一般的な方法を書くことができます

私は、抽象基底クラスを試してみましたが、この手段ました基本クラスにない各エンティティにプロパティを追加する必要があることを示します。それぞれのコレクションがベースタイプのコレクションになっているので、コレクションが失われているように見えますか?

部分的なクラスを追加して共通のインターフェイスから継承できますか?

私は、個々のテーブルに対してそれはないに対する私のすべてのメソッドを書くことができるように、私は「テーブル」と呼ばれる基本クラスまたはインタフェースを持つことができますどのように

Table 1  Table 2  Table 3 
Id   Id   Id 
Month1  Month1  Month1 
Month2  Month2  Month2 
Month3  Month3  Month3 
Quarter2 Quarter2 Quarter2 
Quarter3 Quarter3 Quarter3 
Quarter4 Quarter4 Quarter4 

+0

あなたが別の列としてTABLETYPEを持っていないことはできますか? 1つのテーブルを持つことができます。 – Damith

+0

@DSW:パフォーマンス上の理由から分割されています - テーブルごとに何百万もの行がありますので、別々にしておきたいと思います。 – BlueChippy

+0

これはあなたに役立ちますhttp://blogs.msdn.com/b/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx – Damith

答えて

0

あなたが別々のテーブルを維持するための力で、あなただけの 共通のメソッドを書くことができるようにしたい場合は、インターフェイスと以下のような拡張メソッドを使用することができます。

public interface ITableBase{ 
    int Id{ get; set; } 
    // other properties 
    void Method1(); 
    int Method2(); 
    string Method3(int some_arguments); 
} 

public partial class Table1 : ITableBase{ 
    // implement ITableBase and other properties and methods 
} 

public partial class Table2 : ITableBase{ 
    // implement ITableBase and other properties and methods 
} 

public partial class Table2 : ITableBase{ 
    // implement ITableBase and other properties and methods 
} 

static public class ITableBaseExtensions{ 
    static public string GetIdAsString(this ITableBase table){ 
     return table.Id.ToString(); 
    } 
    static public int UsingMethod2(this ITableBase table){ 
     var i = table.Method2(); 
     return i * 5 + 9 - 14/3 % 7 /* etc... */; 
    } 
    static public void AddingNewMethodToTables(this ITableBase table, string some_string, 
     int some_int, YourType any_other_parameter /* etc... */){ 
     // implement the method you want add to all Table classes 
    } 
    // 
    // You can add any method you want to add to table classes, here, as an Extension method 
    // 
} 

と消費者で、あなたはすべてのテーブルのITableBaseExtensionsクラスで定義されたすべてのメソッドを呼び出すことができます。

public class MyConsumer{ 
    public void MyMethod(){ 
     Table1 t1 = new Table1(); 
     Table1 t2 = new Table2(); 
     Table1 t3 = new Table3(); 

     t1.UsingMethod2(); // will be called from ITableBaseExtensions 
     t1.Method2(); // will be called from ITableBase - must implemented in Table1 class 

     t2.Method3(some_argument); // will be called from ITableBase - must implemented in Table2 class 
     t2.GetIdAsString(); // will be called from ITableBaseExtensions 

     // etc. 
    } 
} 
+0

ありがとう、私は部分クラス定義を繰り返すすべての人を避けることを望んでいましたしかし、これを達成する唯一の賢明な方法と思われる。 – BlueChippy

+0

私はあなたが本当に欲しいものは分かりませんが、EFではTPT、TPH、およびTPC継承を使用できます。また、受け入れのおかげで、答えがあなたを助けてくれたら、投票してください。よろしく。 –

関連する問題