24

Zanim to zostanie oznaczone jako duplikat, sprawdziłem inne powiązane posty i nie odpowiadają one na moje pytanie.mapowanie wielu tabel na jedną klasę encji w strukturze jednostki

Pracuję nad starszą bazą danych, która ma 2 tabele, które mają relację 1: 1. Obecnie mam jeden typ (1Test: 1Result) dla każdej z tych tabel zdefiniowanych Chciałbym scalić te konkretne tabele w jedną klasę.

Obecne typy wyglądać następująco

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; } 
} 

wolałbym je wyglądać ten

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; } 
} 

Obecnie używam Fluent API dla mapowania je do naszej bazy danych Oracle starszych.

Jaka byłaby najlepsza metoda łączenia ich w jedną klasę? Należy pamiętać, że jest to starsza baza danych. Zmiana tabel nie jest opcją i tworzenie widoków nie jest realnym rozwiązaniem w tym momencie projektu.

Odpowiedz

31

Możesz użyć podziału jednostek, aby osiągnąć ten cel, jeśli masz ten sam klucz podstawowy w obu tabelach.

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"); 
     }); 

Oto przydatny article

+1

oni mają ten sam klucz, tak to powinno działać w razie oni nie zrobił to i tak będzie to możliwe przez relacji mapowania oddzielnie lub jest to, że nie jest dostępny w tej wersji ram jeszcze ? –

+0

Jak można zamapować na instrument? – roland

Powiązane problemy