我有兩個模型是一對多的關係相關。第一種模式基本上存儲患者信息(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
被解讀爲fileId
,fileId
到parFileId
。
換句話說,即使模型中不存在,我如何將fullname
的值與其他值一起添加到PatPar
中?我需要一個property
嗎?
澄清更新
我在做基本上有三種選擇
首先我選擇使用Id
第二PatPar
有相關數據從PatReg
記錄,並得到使用相同的Id
選擇我的問題是fullname
,我需要將其包含在PatPar中,但使用不同的密鑰選擇
第三個選擇應該parFileId
在PatPar
在PatReg
去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,
}]
}
}
「全名」在'PatReg'存在不'PatPar',爲了得到它,聲明應該是這樣'那裏(c.ParFileId == ID)' – JSON
相應的編輯 –
我需要有兩個DTO獲得如上所示的輸出?或者我可以使用DTO來填充'公共ICollection PatPar {get;組; }'PatReg'模型中? –
JSON