簡體   English   中英

將多個表映射到實體框架中的單個實體 class

[英]mapping multiple tables to a single entity class in entity framework

在將其標記為重復之前,我已經檢查了其他相關帖子,但他們沒有回答我的問題。

我正在研究具有 2 個具有 1:1 關系的表的舊數據庫。 目前,我為這些表中的每一個定義了一種類型 (1Test:1Result) 我想將這些特定表合並到一個 class 中。

當前類型如下所示

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

我希望它們看起來像這樣

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

我目前正在使用流利的 API 將這些映射到我們的舊版 Oracle 數據庫。

將這些組合成單個 class 的最佳方法是什么? 請注意,這是一個遺留數據庫。 更改表格不是一種選擇,並且在項目的這一點上創建視圖不是一個可行的解決方案。

如果兩個表中的主鍵相同,則可以使用實體拆分來實現此目的。

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

這是一篇有用的文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM