自动映射器表达式将一个属性映射到多个属性

编程入门 行业动态 更新时间:2024-10-27 21:17:45
本文介绍了自动映射器表达式将一个属性映射到多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在运行WebAPI

I'm running a WebAPI,

Asp.Net Core 3.1 Automapper: 9.0 Automapper.Extensions.ExpressionMapping: 3.1

我的LocaleDto PK是2 FK的复合键- LanguageCode 和 CountryCode

My LocaleDto PK is a composite key of 2 FK - LanguageCode and CountryCode

public class LocaleDto { [Key, ForeignKey(nameof(Language)), Column(Order = 0)] public string LanguageCode { get; set; } public LanguageDto Language { get; set; } [Key, ForeignKey(nameof(Country)), Column(Order = 1)] public string CountryCode { get; set; } public CountryDto Country { get; set; } }

我想将LocaleDto映射到LocaleViewModel,其中 Id 是基于以下模式 languageCode-CountryCode 构建的. en-GB

I would like to map LocaleDto to LocaleViewModel, where Id is build based on the following pattern languageCode-CountryCode ie. en-GB

public class LocaleViewModel { public string Id {get;set;} public string LanguageCode { get; set; } public LanguageViewModel Language { get; set; } public string CountryCode { get; set; } public CountryViewModel Country { get; set; } }

当我使用静态助手方法将一个对象映射到另一个对象时,以下映射工作正常:

The following mapping works just fine when I map one object to another using a static helper method:

CreateMap<LocaleDto, LocaleViewModel>() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => LocalisationHelper.ToLocaleCode(src.LanguageCode, src.CountryCode))); CreateMap<LocaleViewModel, LocaleDto>() .ForMember(dest => dest.LanguageCode, src => src.MapFrom(x => x.LanguageCode)) .ForMember(dest => dest.CountryCode, src => src.MapFrom(x => x.CountryCode)); ... public static string ToLocaleCode(string languageCode, string countryCode) { if (!string.IsNullOrEmpty(languageCode) && !string.IsNullOrEmpty(countryCode)) { return $"{languageCode}-{countryCode}"; } return null; }

但是当我映射表达式时,结果在LINQ中无法理解为SQL,因为它包含帮助程序LocalisationHelper.ToLocaleCode.

But when I map the expression, the result is not understandable in LINQ to SQL, cause it contains the helper LocalisationHelper.ToLocaleCode.

Expression<Func<LocaleViewModel, bool>> filter = x => x.Id == "en-GB"; var entityFilter = mapper.MapExpression<Expression<Func<LocaleDto, bool>>>(filter);

检查EntityFilter:

{Param_0 => (ToLocaleCode(Param_0.LanguageCode, Param_0.CountryCode) == "en-GB")}

例外

The LINQ expression 'DbSet<LocaleDto> .Where(l => LocalisationHelper.ToLocaleCode( languageCode: l.LanguageCode, countryCode: l.CountryCode) == "en-GB")' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See go.microsoft/fwlink/?linkid=2101038 for more information.

是否可以在Automapper中以不同方式映射这些属性,以便将结果转换为SQL,而无需切换到客户端评估?

推荐答案

如果您尝试使用静态函数而不是静态函数,该怎么办

What if you tried instead of using a static function evaluate the expression directly

CreateMap<LocaleDto, LocaleViewModel>() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.LanguageCode + "-" + src.CountryCode));

更多推荐

自动映射器表达式将一个属性映射到多个属性

本文发布于:2023-11-17 05:39:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   多个   表达式   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!