2017-09-05 57 views
0

概念如何結合使用EF的兩個相關模型的相關數據?

我有兩個模型是一對多的關係相關。第一種模式基本上存儲患者信息(PatReg)。第二個模型加入合作伙伴(PatPar)。即(FileId = 1Sam,FileId = 2維多利亞,FileId = 3傑西卡,FileId = 4Micaica)信息全部存儲在PatReg中。以說,山姆是維多利亞和傑西卡的合作伙伴我加入他們的行列中PatPar以下方式:

FileId= 1 FileId=2 
    FileId= 1 FileId=3 

以下狀態這兩種模式。

public class PatReg 
    { 
     public Int64 FileId { get; set; } 
     [Required, Display(Name = "First Name")] 
     public string FName { get; set; } 
     [Required, Display(Name = "Middle Name")] 
     public string MName { get; set; } 
     [Required, Display(Name = "Last Name")] 
     public string LName { get; set; } 
     [Display(Name = "Full Name"), NotMapped] 
     public string fullname 
     { 
      get { return FName + " " + MName + " " + LName; } 
     } 
     [Required, Display(Name = "Date of Birth")] 
     [DataType(DataType.Date)] 
     public DateTime Dob { get; set; } 
     public ICollection<PatPar> PatPar { get; set; } 
    } 

    public class PatPar 

    { 
     [Key] 
     public Int64 RecId { get; set; } 
     [Display(Name = "Patient File Id"), Required] 
     public Int64 FileId { set; get; } 
     [Display(Name = "Partner File Id"), Required] 
     public Int64 ParFileId { set; get; } 
     [Display(Name = "Start Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true), Required] 
     public DateTime SDate { set; get; } 
     [Display(Name = "End Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     public DateTime? EDate { set; get; } 
    } 

快速檢索 我米用在我的項目,我查詢我的記錄如下方式的API controller

[HttpGet("{id}")] 
    public async Task<IActionResult> GetPatReg([FromRoute] long id) 
    { 

     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 
     var patReg = await _context.PatReg.SingleOrDefaultAsync(m => m.FileId == id);  // here I get my Main Record 
     var patRegPar = await _context.PatPar // here I get related Records 
      .Where(m => m.FileId == id) 
      .ToListAsync(); 
     if (patReg == null) 
     { 
      return NotFound(); 
     } 

     var DataRes = new { 
      sdata = patReg     
     }; 

     return Ok(DataRes); 
    } 

所得JSON

{ 
    "sdata": { 
     "fileId": 1708010001, 
     "fName": "Json", 
     "mName": "S", 
     "lName": "Makenzi", 
     "fullname": "Json S Abu Makenzi", 
     "dob": "1984-04-26T00:00:00", 
     "patPar": [{ 
      "recId": 2, 
      "fileId": 1708010001, 
      "parFileId": 1708010002, 
      "sDate": "1999-12-12T00:00:00", 
      "eDate": null, 
     }, { 
      "recId": 3, 
      "fileId": 1708010001, 
      "parFileId": 1708010003, 
      "sDate": "1955-12-14T00:00:00", 
      "eDate": null, 
     }] 
    } 
} 

我想要我的JSON輸出噸patPar列表看起來像每個記錄如下,

"patPar": [{ 
       "recId": 2, 
       "fullname": "Json S Abu Makenzi", 
       "fileId": 1708010001, 
       "parFileId": 1708010002, 
       "sDate": "1999-12-12T00:00:00", 
       "eDate": null, 
      }, { 
       "recId": 3, 
       "fullname": "Sarah S Maz", 
       "fileId": 1708010001, 
       "parFileId": 1708010003, 
       "sDate": "1955-12-14T00:00:00", 
       "eDate": null, 
      }] 

問題,fullname存在的主要模式PatReg但可以通過鏈接記錄,而不是fileId被解讀爲fileIdfileIdparFileId

換句話說,即使模型中不存在,我如何將fullname的值與其他值一起添加到PatPar中?我需要一個property嗎?

澄清更新

我在做基本上有三種選擇

首先我選擇使用Id 第二PatPar有相關數據從PatReg記錄,並得到使用相同的Id

選擇我的問題是fullname,我需要將其包含在PatPar中,但使用不同的密鑰選擇

第三個選擇應該parFileIdPatParPatReg

FileID相當於SQL是

第一選擇:

SELECT FileId, FName, LName, Dob FROM PatReg Where FileId=id 

第二和第三選擇:

SELECT  PatReg.FileId, PatReg.FName, PatReg.MName, PatReg.LName, PatPar.EDate, PatPar.ParFileId, PatPar.SDate, PatReg_1.FName AS PFName, PatReg_1.MName AS PMName, PatReg_1.LName AS PLName 
FROM   PatPar INNER JOIN 
         PatReg ON PatPar.FileId = PatReg.FileId INNER JOIN 
         PatReg AS PatReg_1 ON PatPar.ParFileId = PatReg_1.FileId 

更新

期望JSON是

{ 
    "sdata": { 
     "fileId": 1708010001, 
     "fName": "**", 
     "mName": "**", 
     "lName": "**", 
     "fullname": "***", 
     "dob": "1984-04-26T00:00:00", 
     "patPar": [{ 
       "recId": 2, 
       "fullname": "*****", 
       "fileId": 1708010001, 
       "parFileId": 1708010002, 
       "sDate": "1999-12-12T00:00:00", 
       "eDate": null, 
      }, { 
       "recId": 3, 
       "fullname": "*****", 
       "fileId": 1708010001, 
       "parFileId": 1708010003, 
       "sDate": "1955-12-14T00:00:00", 
       "eDate": null, 
      }] 
    } 
} 

回答

4

它使用的DTO(數據傳輸對象),以兩個端點之間傳輸數據的好做法。通過這種方式,您可以指定要分享的屬性,根據需要編輯模型,並且可以擺脫那些您不會使用的嘈雜屬性。在這種情況下,你可以添加一個類象下面這樣:

public class PatParDto 

{ 
    public int RecId { get; set; } 
    public int FileId { get; set; } 
    public int ParFileId { get; set; } 
    public DateTime SDate { get; set; } 
    public DateTime? EDate { get; set; } 

    public string FullName {get; set;} 
} 

,然後在你的LINQ查詢,你可以選擇你的PatPar爲PatPatDto:相反

var patRegPar = await _context.PatPar 
      .Where(m => m.FileId == id) 
      .Select(m => new PatParDto { 

      //Here you can set properties 
      FullName = patReg.fullname, 
      RecId = m.RecId 

      }) 
      .ToListAsync(); 

設置屬性的手動你可以使用像庫AutoMapper

+0

「全名」在'PatReg'存在不'PatPar',爲了得到它,聲明應該是這樣'那裏(c.ParFileId == ID)' – JSON

+0

相應的編輯 –

+0

我需要有兩個DTO獲得如上所示的輸出?或者我可以使用DTO來填充'公共ICollection PatPar {get;組; }'PatReg'模型中? – JSON

1

我seggestion是從虛擬財產作出匿名類型,當我們使用相關的表:

var patRegPar = await _context.PatPar // here I get related Records 
     .Include(c=>c.PatReg) 
     .Where(m => m.FileId == id) 
     .Select(t=>new{ recId = t.recId , fullname = t.PatReg.fullname , ... }) 
     .ToListAsync(); 
0

感謝Ege Tuncoz

我加了DTO

public class PatParDto 

{ 
    public int RecId { get; set; } 
    public int FileId { get; set; } 
    public int ParFileId { get; set; } 
    public DateTime SDate { get; set; } 
    public DateTime? EDate { get; set; } 

    public string FullName {get; set;} 
} 

然後在我的控制器我跑了一個循環來添加所需的值在DTO每個記錄。

 [HttpGet("{id}")] 
     public async Task<IActionResult> GetPatReg([FromRoute] long id) 
     { 

        if (!ModelState.IsValid) 
        { 
         return BadRequest(ModelState); 
        } 
        var patReg = await _context.PatReg 
         .Where(m => m.FileId == id) 
         .ToListAsync(); 

        var patpar = await _context.PatPar.Select(m => new PatParDto { 
         RecId = m.RecId, 
         FileId = m.FileId, 
         ParFileId = m.ParFileId, 
         SDate = m.SDate, 
         EDate = m.EDate, 
        }).ToListAsync(); 
        for (int i = 0; i < patpar.Count; i++) 

        { 
         patpar[i].FullName = (from a in _context.PatReg 
         where (a.FileId == patpar[i].ParFileId) 
               select new { a.fullname } 
              ).Single().fullname; 

            or 

         patpar[i].FullName = _context.PatReg.Where(a => 
         a.FileId == patpar[i].ParFileId) 
             .Select(t=>new {t.fullname }) 
             .Single().fullname;          

        } 


        if (patReg == null) 
        { 
         return NotFound(); 
        } 

        var DataRes = new { 
         sdata = patReg, 
         test= patpar 
        }; 

        return Ok(DataRes); 
     }