24

これが重複としてマークされる前に、私は他の関連する投稿をチェックして、私の質問に答えません。複数のテーブルをエンティティフレームワーク内の単一のエンティティクラスにマッピングする

私は、1対1の関係を持つ2つのテーブルを持つレガシーデータベースを作成しています。 現在、これらのテーブルごとに1つのタイプ(1Test:1Result)が定義されています これらの特定のテーブルを1つのクラスにマージしたいと思います。

現在の種類は、私は私は現在、当社のレガシーのOracleデータベースにこれらをマッピングするための流暢APIを使用しています。この

public class TestResult 
{ 
    public   int  Id    { get; set; } 
    public   string  Status   { get; set; } 
    public   string  Analysis  { get; set; } 
    public   string  ComponentList { get; set; } 
    public   string  TestName  { get; set; } 
    public   string  Text   { get; set; } 
    public   string  Units   { get; set; } 
    public   bool  OutOfRange  { get; set; } 
    public   string  Status   { get; set; } 
    public   string  Minimum   { get; set; } 
    public   string  Maximum   { get; set; } 

    public virtual Instrument InstrumentUsed { get; set; } 
} 

のように見えるためにそれらを好むこの

public class Result 
{ 
    public   string  Id     { get; set; } 
    public   string  Name    { get; set; } 
    public   string  Text    { get; set; } 
    public   string  Units    { get; set; } 
    public   bool  OutOfRange   { get; set; } 
    public   string  Status    { get; set; } 
    public   string  Minimum    { get; set; } 
    public   string  Maximum    { get; set; } 

    public virtual Instrument InstrumentUsed  { get; set; } 

    public virtual Test  ForTest    { get; set; } 
} 


public class Test 
{ 
    public   int  Id   { get; set; } 
    public   string Status  { get; set; } 
    public   string Analysis  { get; set; } 
    public   string ComponentList { get; set; } 
    public virtual Sample ForSample  { get; set; } 
    public virtual Result TestResult { get; set; } 
} 

のように見えます。

これらを1つのクラスに結合する最良の方法は何でしょうか? これは従来のデータベースです。テーブルの変更はオプションではなく、ビューを作成することはプロジェクトの現時点で実行可能な解決策ではありません。

答えて

31

エンティティ分割を使用して、両方のテーブルに同じプライマリキーがある場合は、これを実現できます。

modelBuilder.Entity<TestResult>() 
    .Map(m => 
     { 
     m.Properties(t => new { t.Name, t.Text, t.Units /*other props*/ }); 
     m.ToTable("Result"); 
     }) 
    .Map(m => 
     { 
     m.Properties(t => new { t.Status, t.Analysis /*other props*/}); 
     m.ToTable("Test"); 
     }); 

ここで彼らは、これは、彼らはそれがまだ別々にマッピング関係によって可能であるか、まだフレームワークのこのバージョンではその利用できないでしょうdidntのイベントで動作するはずですので、同じキーを持っている便利article

+1

です? –

+0

どのように楽器にマッピングしますか? – roland

関連する問題