第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Entity Framework Core 中將字典轉(zhuǎn)換為 ICollection

在 Entity Framework Core 中將字典轉(zhuǎn)換為 ICollection

C#
守著星空守著你 2022-12-24 13:37:41
在這里,我對(duì)EF Core中的dictionaryto轉(zhuǎn)換感到困惑。Icollection我Dictionary在FlatEmployee課堂上將鍵值對(duì)列表存儲(chǔ)在數(shù)據(jù)庫(kù)中。我這樣聲明:public class FlatEmployee{    public int EmployeeId { get; set; }    public Dictionary<string, long> PayAndAllowances { get; set; }}//====================Configuration public void Configure(EntityTypeBuilder<FlatEmployee> builder){        builder.HasKey(f => new { f.EmployeeId });        builder.Property(sb => sb.PayAndAllowances)            .HasConversion(                pa => JsonConvert.SerializeObject(pa),                pa => JsonConvert.DeserializeObject<Dictionary<string, long>>(pa));}當(dāng)我播種或插入時(shí),這絕對(duì)沒(méi)問(wèn)題。但是我在嘗試獲取 FlatEmployee 類(lèi)時(shí)遇到的問(wèn)題。這是因?yàn)槲蚁氆@取 Collection 中的字典。為此,我聲明了另一個(gè)這樣的類(lèi):public class LastPayCertificateViewModel: IHaveCustomMapping{    public int EmployeeCode { get; set; }    public EmployeeEarningDTO PayAndAllowances { get; set; }    public void CreateMappings(Profile configuration)    {        configuration.CreateMap<FlatEmployee, LastPayCertificateViewModel>()            .ForMember(dto => dto.EmployeeCode , opt => opt.MapFrom(entity => entity.EmployeeId ));    }}public class EmployeeEarningDTO : IHaveCustomMapping{    public ICollection<BaseEmployeeDictionary> PayAndAllowances { get; set; }    public void CreateMappings(Profile configuration)    {        configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>()            .ForMember(dto => dto.PayAndAllowances, opt => opt.MapFrom(entity => entity.PayAndAllowances));    }}public class BaseEmployeeDictionary{    public string Name { get; set; }    public long Amount { get; set; }}當(dāng)我嘗試使用上面的類(lèi)來(lái)獲取這樣的數(shù)據(jù)時(shí):public class LastPayCertificateQuery : IRequest<LastPayCertificateViewModel>{    public string EmployeeCode { get; set; }}
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

原來(lái)是AutoMapper映射問(wèn)題。


首先,與以下相比,EmployeeEarningDTO內(nèi)部創(chuàng)建了額外的級(jí)別:LastPayCertificateViewModelFlatEmployee


LastPayCertificateViewModel.PayPayAndAllowances.PayAndAllowances

對(duì)比


FlatEmployee.PayAndAllowances

AutoMapper 默認(rèn)映射具有相同名稱(chēng)的屬性。所以在FlatEmployeemap內(nèi)部LastPayCertificateViewModel它會(huì)嘗試映射Dictionary<string, long> PayAndAllowances到EmployeeEarningDTO PayAndAllowances. 但是沒(méi)有從Dictionary<string, long>到的映射EmployeeEarningDTO。相反,有一個(gè)從FlatEmployee到的映射EmployeeEarningDTO,所以你必須告訴 AM 使用它:


configuration.CreateMap<FlatEmployee, LastPayCertificateViewModel>()

    .ForMember(dto => dto.EmployeeCode, opt => opt.MapFrom(entity => entity.EmployeeId))

    .ForMember(dto => dto.PayAndAllowances, opt => opt.MapFrom(entity => entity)); // <--

其次,映射 from FlatEmployeeto EmployeeEarningDTO- AM 會(huì)自動(dòng)嘗試映射PayAndAllowances屬性,但是沒(méi)有映射 from KeyValuePair<string, long>to BaseEmployeeDictionary。你可以定義這樣的映射


configuration.CreateMap<KeyValuePair<string, long>, BaseEmployeeDictionary>()

                        .ForMember(dst => dst.Name, opt => opt.MapFrom(src => src.Key))

                        .ForMember(dst => dst.Amount, opt => opt.MapFrom(src => src.Value));

這將使您可以簡(jiǎn)單地使用


configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>();

但是您可能不會(huì)這樣做,因?yàn)槟幌M總€(gè)都 KeyValuePair<string, long>映射到BaseEmployeeDictionary,因此您可以就地進(jìn)行映射:


configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>()

     .ForMember(dto => dto.PayAndAllowances, opt => opt.MapFrom(entity => entity.PayAndAllowances

        .Select(src => new BaseEmployeeDictionary { Name = src.Key, Amount = src.Value })));



查看完整回答
反對(duì) 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)