我有以下代码:
var data = repo.GetAll();
Mapper.CreateMap<Products_GetAll_Result, Product>();
return Mapper.Map<IEnumerable<Products_GetAll_Result>, List<FurnitureStore.Models.Product>>(data.ToList());
Run Code Online (Sandbox Code Playgroud)
我在异常中收到以下消息:
{"Missing type map configuration or unsupported mapping.
Mapping types:
Products_GetAll_Result -> Product
FurnitureStore.DataLayer.Products_GetAll_Result -> FurnitureStore.Models.Product
Destination path:
List`1[0]
Source value:
FurnitureStore.DataLayer.Products_GetAll_Result"}
Run Code Online (Sandbox Code Playgroud)
我尝试了我能想到的一切,但我无法让它发挥作用。我在这里做错了什么?
public partial class Source()
{
...............
public string Assignees { get; set; }
...................
}
public partial class Destination
{
...............
public List<int> Resources { get; set; }
...................
}
Run Code Online (Sandbox Code Playgroud)
我像这样映射这些类
Mapper.CreateMap<Source, Destination>().ForMember(x => x.DestID, y => y.MapFrom(z => z.SrcID));//Automapper version 4.2.1.0
Run Code Online (Sandbox Code Playgroud)
我得到了所有值的预期结果,但问题在于 Source 中的字段Assignees,它是逗号分隔的字符串。它包含类似“1,4,6,8”的数据
我期望什么:
我希望它们在映射发生时转换为int 列表。
请提供任何有价值的意见。谢谢。
测试使用:Automapper 10.1.2-alpha.0.4(MyGet Build)
我们希望将 DomainEvents 映射到没有任何基类的公共合约,并将它们发布到消息队列/主题。
由于它应该是动态映射,因此在映射到目标合约时我们不知道目标类型。
对于这种情况,我们在这篇文章中提供了以下解决方案: 如何使用 Automapper 将对象映射到未知的目标类型?如下:
public async Task Publish(DomainEvent domainEvent)
{
this.logger.LogInformation("Publishing domain event. Event - {event}", domainEvent.GetType().Name);
try
{
var publicEvent = this.MapToContract(domainEvent);
await this.publishEndpoint.Publish(publicEvent);
}
catch (Exception ex)
{
this.logger.LogError("Unexpected exception while publishing domain event.", ex);
}
}
private object MapToContract(object source)
{
var sourceType = source.GetType();
TypeMap? typeMap = null;
try
{
typeMap = this.mapper.ConfigurationProvider
.GetAllTypeMaps()
.SingleOrDefault(map => map.SourceType == sourceType);
}
catch (InvalidOperationException)
{
throw new Exception($"Multiple types for …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 AutoMapper 映射 DTO Dictionary<string, object>:
public class SetEmr
{
public string Name { get; set; }
public int Repeat { get; set; }
public int RestTime { get; set; }
public int Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试做这样的事情:
CreateMap<SetEmr, Dictionary<string, object>>()
.ForMember(dest => dest, opt => opt.MapFrom((src, dst, _, context) =>
{
return new Dictionary<string, object>
{
{ "Name", src.Name },
{ "Repeat", src.Repeat},
{ "Order", src.Order}
};
}));
Run Code Online (Sandbox Code Playgroud)
这不起作用。
“只有类型上的顶级个人成员才支持成员的自定义配置。”
还有其他方法可以实现这样的映射吗?