【发布时间】:2021-04-28 17:04:54
【问题描述】:
如何通过外键从其他表中映射值来解决问题?我有 2 个表(客户、维修)。
public class Repair
{
[Key]
public int Id { get; set; }
[Required]
public string Warranty { get; set; }
[Required]
public string Description { get; set; }
[ForeignKey(nameof(Client))]
public int ClientId { get; set; }
}
public class RepairReadDto
{
public int Id { get; set; }
public string Warranty { get; set; }
public string Description { get; set; }
public int ClientId { get; set; }
}
现在的响应如下所示: [ { “身份证”:1, “保修”:“3”, “描述”:“aaaa”, “客户标识”:1, } ]
可以通过外键从其他表中获取值吗?例如,我期望这样的输出:
[
{
"id": 1,
"warranty": "3",
"description": "aaaa",
"client":{"ClientId": 1, "Name": Example, "Surname": Example, "Phone": 1234567
}
]
【问题讨论】:
-
是的,这是可能的。但是你没有提到你有问题吗?是在 AutoMapper 中还是在 EntityFramework 中?
-
问题是我认为使用自动映射器,我的地图看起来像这样: CreateMap
();我还需要做点什么吗?
标签: c# asp.net sql-server entity-framework automapper