c# - 地图列表列出带有 .net 核心的自动映射器
问题描述
我有两个模型。第一个是:
public class Push
{
public int PushId { get; set; }
public Customer Customer { get; set; }
public int CustomerId { get; set; }
public string PackageId { get; set; }
public string Category { get; set; }
public string Advertiser { get; set; }
public PushTemplate PushTemplate { get; set; }
public string TemplateType { get; set; }
public string IntervalType { get; set; }
public int TopDateTimeBorder{ get; set; }
public int BottomDateTimeBorder { get; set; }
public DateTime ClientStartDateTime { get; set; }
public string LangCode { get; set; }
public string PushTitle { get; set; }
public string PushBody { get; set; }
public string FCMResponse { get; set; }
public bool Sent { get; set; }
public DateTime CreatedAt { get; set; }
public Distribution Distribution { get; set; }
public Push()
{
CreatedAt = DateTime.UtcNow;
}
}
第二个是:
public class FailedPush
{
public int FailedPushId { get; set; }
public Customer Customer { get; set; }
public int CustomerId { get; set; }
public string PackageId { get; set; }
public string Category { get; set; }
public string Advertiser { get; set; }
public PushTemplate PushTemplate { get; set; }
public string TemplateType { get; set; }
public string IntervalType { get; set; }
public int TopDateTimeBorder{ get; set; }
public int BottomDateTimeBorder { get; set; }
public DateTime ClientStartDateTime { get; set; }
public string LangCode { get; set; }
public string PushTitle { get; set; }
public string PushBody { get; set; }
public string FCMResponse { get; set; }
public DateTime CreatedAt { get; set; }
public Distribution Distribution { get; set; }
public FailedPush()
{
CreatedAt = DateTime.UtcNow;
}
}
如果属性为假,我想FailedPush
从模型列表中获取模型列表。Push
Sent
所以我有以下代码:
var failedPushes = _mapper.Map<List<Push>, List<FailedPush>>(await pushes.Where(x => !x.Sent).ToListAsync());
但是failedPushes
是空的。注入 mapper I 用户IMapper
界面:
// class prop
private readonly IMapper _mapper;
...
// constructor
public ScopedProcessingService(IMapper mapper)
{
...
_mapper = mapper;
...
}
在Startup.cs
课堂上,我有以下内容:
services.AddAutoMapper(typeof(Startup));
那么有什么问题呢?
解决方案
您必须创建映射类的配置。
Automapper 会自动搜索继承 Profile 的类来创建配置。
创建一个文件夹 Profile 并将您的配置放在那里。
public class PushProfile : Profile
{
public PushProfile()
{
CreateMap<Push, FailedPush>();
}
}
推荐阅读
- javascript - 有没有人将 Test Rail 与 Cypress 集成在一起?我正在寻找自动更新测试用例的状态
- c# - c#中的指针char*应该传递什么参数?
- java - 如何从java中的扫描仪输入中拆分字符串
- java - 即使我更新了它正在检查的变量,我的程序仍继续运行相同的 if 语句
- python - 为什么我的时间序列数据框会限制我想在 LSTM 中使用的时间步数?
- asp.net - HTTP 错误 500.30 - 使用 app.UseStaticFiles 时 ANCM 进程内启动失败 asp.net.Core3.1
- python - Python - 从类中调用函数时出错
- fabricjs - 如何复制 IText 样式以获得明确的字符
- rust - 两个 NEAR 协议智能合约之间的交叉通信命名结构是如何确定的?
- mysql - 是否有更快的方法从表中选择记录并查询其子表