自动映射器表达式将一个属性映射到多个属性 [英] Automapper Expression mapping one property to many
问题描述
我正在运行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 https://go.microsoft.com/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));
这篇关于自动映射器表达式将一个属性映射到多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!