AutoMapper - 如何 return 来自外键的值?
AutoMapper - how to return values from foreign key?
如何通过外键使用来自其他 table 的地图值解决问题?我有 2 tables(客户,维修)。
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”,
“clientId”:1,
}
]
是否可以通过外键从其他 table 获取值?例如我期望这样的输出:
[
{
"id": 1,
"warranty": "3",
"description": "aaaa",
"client":{"ClientId": 1, "Name": Example, "Surname": Example, "Phone": 1234567
}
]
我认为像下面这样的东西会对你有所帮助。基本上,Auto Mapper 也可以映射子对象,但是应该定义它并且应该相应地创建 类。
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 virtual Client Client { 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; }
public ClientDto Client { get; set; }
}
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ClientDto
{
public int Id { get; set; }
public string Name { get; set; }
}
在 AutoMapper 上
CreateMap<Repair, RepairReadDto>()..ReverseMap();
CreateMap<Client, ClientDto>()..ReverseMap();
好的,现在如果我使用两个不同的表(客户端、修复),我需要在每个表上导入存储库并在控制器中从修复中填充“客户端”。
public ActionResault<IEnumerable<RepairReadDto>> GetRepairList()
{
var repairList = _repairRepo.GetProductList().Tolist();
foreach(var repair from repairList)
{
repair.Client = _mapper.Map<ClientReadDto>(_clientRepo.GetClientById(repair.ClientId))
}
return Ok(repairList);
}
还有其他约定吗?
如何通过外键使用来自其他 table 的地图值解决问题?我有 2 tables(客户,维修)。
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”, “clientId”:1, } ]
是否可以通过外键从其他 table 获取值?例如我期望这样的输出:
[
{
"id": 1,
"warranty": "3",
"description": "aaaa",
"client":{"ClientId": 1, "Name": Example, "Surname": Example, "Phone": 1234567
}
]
我认为像下面这样的东西会对你有所帮助。基本上,Auto Mapper 也可以映射子对象,但是应该定义它并且应该相应地创建 类。
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 virtual Client Client { 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; }
public ClientDto Client { get; set; }
}
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ClientDto
{
public int Id { get; set; }
public string Name { get; set; }
}
在 AutoMapper 上
CreateMap<Repair, RepairReadDto>()..ReverseMap();
CreateMap<Client, ClientDto>()..ReverseMap();
好的,现在如果我使用两个不同的表(客户端、修复),我需要在每个表上导入存储库并在控制器中从修复中填充“客户端”。
public ActionResault<IEnumerable<RepairReadDto>> GetRepairList()
{
var repairList = _repairRepo.GetProductList().Tolist();
foreach(var repair from repairList)
{
repair.Client = _mapper.Map<ClientReadDto>(_clientRepo.GetClientById(repair.ClientId))
}
return Ok(repairList);
}
还有其他约定吗?