在 Entity Framework Core 中将字典转换为 ICollection
Convert Dictionary to ICollection in Entity Framework Core
在这里,我遇到了在 EF Core 中将 dictionary
转换为 Icollection
的问题。我在 FlatEmployee
class 中有 Dictionary
,我在其中存储数据库中的键值对列表。我这样声明:
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));
}
这在我播种或插入时非常有效。但是我在尝试获取 FlatEmployee class 时遇到的问题。这是因为我想获取 Collection 中的字典。为此,我声明了另一个 class 这样的:
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; }
}
当我尝试使用上面的 classes 来获取这样的数据时:
public class LastPayCertificateQuery : IRequest<LastPayCertificateViewModel>
{
public string EmployeeCode { get; set; }
}
public async Task<LastPayCertificateViewModel> Handle(LastPayCertificateQuery request, CancellationToken cancellationToken)
{
var predicate = PredicateBuilder.False<FlatEmployee>();
predicate = predicate.Or(emp => emp.EmployeeId == request.EmployeeCode);
var employee = await _context.FlatEmployee
.Where(predicate)
.FirstOrDefaultAsync(cancellationToken);
if (employee == null)
return null;
var empVM = _mapper.Map<LastPayCertificateViewModel>(employee);
}
然后我在 PayAndAllowances in empVM
中得到 null。这就是我的问题所在。我的问题在哪里?我认为这是因为该词典具有键值对并且无法转换为 BaseEmployeeDictionary
。我也尝试过这种方式将列表项添加到 PayAndAllowances in empVM
foreach (KeyValuePair<string, long> data in employee.DeductionsByAdjustment)
{
BaseEmployeeDictionary listItem = new BaseEmployeeDictionary
{
Name = data.Key,
Amount = data.Value
};
empVM.EarningDetails.PayAndAllowances.Add(listItem);
return empVM;
}
这当然不会起作用,因为 empVM.EarningDetails.PayAndAllowances
为 null 并抛出 NullReferenceException。我的问题是如何在 EmployeeEarningDTO
中创建映射时将字典映射到 ICollection。或者 如果您提出宝贵的建议和解决方案,我们将不胜感激。
原来是AutoMapper映射问题
首先,LastPayCertificateViewModel
中的 EmployeeEarningDTO
与 FlatEmployee
相比创建了额外的级别:
LastPayCertificateViewModel.PayPayAndAllowances.PayAndAllowances
对
FlatEmployee.PayAndAllowances
AutoMapper 默认映射具有相同名称的属性。所以在 FlatEmployee
到 LastPayCertificateViewModel
的映射中,它会尝试将 Dictionary<string, long> PayAndAllowances
映射到 EmployeeEarningDTO PayAndAllowances
。但是没有从Dictionary<string, long>
到EmployeeEarningDTO
的映射。相反,有一个从 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)); // <--
其次,从FlatEmployee
映射到EmployeeEarningDTO
- AM会自动尝试映射PayAndAllowances
属性,但是没有从KeyValuePair<string, long>
映射到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));
这将使您可以简单地使用
configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>();
但是你可能不会这样做,因为你不希望 每个 KeyValuePair<string, long>
都映射到 BaseEmployeeDictionary
,所以你可以这样做就地映射:
configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>()
.ForMember(dto => dto.PayAndAllowances, opt => opt.MapFrom(entity => entity.PayAndAllowances
.Select(src => new BaseEmployeeDictionary { Name = src.Key, Amount = src.Value })));
在这里,我遇到了在 EF Core 中将 dictionary
转换为 Icollection
的问题。我在 FlatEmployee
class 中有 Dictionary
,我在其中存储数据库中的键值对列表。我这样声明:
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));
}
这在我播种或插入时非常有效。但是我在尝试获取 FlatEmployee class 时遇到的问题。这是因为我想获取 Collection 中的字典。为此,我声明了另一个 class 这样的:
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; }
}
当我尝试使用上面的 classes 来获取这样的数据时:
public class LastPayCertificateQuery : IRequest<LastPayCertificateViewModel>
{
public string EmployeeCode { get; set; }
}
public async Task<LastPayCertificateViewModel> Handle(LastPayCertificateQuery request, CancellationToken cancellationToken)
{
var predicate = PredicateBuilder.False<FlatEmployee>();
predicate = predicate.Or(emp => emp.EmployeeId == request.EmployeeCode);
var employee = await _context.FlatEmployee
.Where(predicate)
.FirstOrDefaultAsync(cancellationToken);
if (employee == null)
return null;
var empVM = _mapper.Map<LastPayCertificateViewModel>(employee);
}
然后我在 PayAndAllowances in empVM
中得到 null。这就是我的问题所在。我的问题在哪里?我认为这是因为该词典具有键值对并且无法转换为 BaseEmployeeDictionary
。我也尝试过这种方式将列表项添加到 PayAndAllowances in empVM
foreach (KeyValuePair<string, long> data in employee.DeductionsByAdjustment)
{
BaseEmployeeDictionary listItem = new BaseEmployeeDictionary
{
Name = data.Key,
Amount = data.Value
};
empVM.EarningDetails.PayAndAllowances.Add(listItem);
return empVM;
}
这当然不会起作用,因为 empVM.EarningDetails.PayAndAllowances
为 null 并抛出 NullReferenceException。我的问题是如何在 EmployeeEarningDTO
中创建映射时将字典映射到 ICollection。或者 如果您提出宝贵的建议和解决方案,我们将不胜感激。
原来是AutoMapper映射问题
首先,LastPayCertificateViewModel
中的 EmployeeEarningDTO
与 FlatEmployee
相比创建了额外的级别:
LastPayCertificateViewModel.PayPayAndAllowances.PayAndAllowances
对
FlatEmployee.PayAndAllowances
AutoMapper 默认映射具有相同名称的属性。所以在 FlatEmployee
到 LastPayCertificateViewModel
的映射中,它会尝试将 Dictionary<string, long> PayAndAllowances
映射到 EmployeeEarningDTO PayAndAllowances
。但是没有从Dictionary<string, long>
到EmployeeEarningDTO
的映射。相反,有一个从 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)); // <--
其次,从FlatEmployee
映射到EmployeeEarningDTO
- AM会自动尝试映射PayAndAllowances
属性,但是没有从KeyValuePair<string, long>
映射到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));
这将使您可以简单地使用
configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>();
但是你可能不会这样做,因为你不希望 每个 KeyValuePair<string, long>
都映射到 BaseEmployeeDictionary
,所以你可以这样做就地映射:
configuration.CreateMap<FlatEmployee, EmployeeEarningDTO>()
.ForMember(dto => dto.PayAndAllowances, opt => opt.MapFrom(entity => entity.PayAndAllowances
.Select(src => new BaseEmployeeDictionary { Name = src.Key, Amount = src.Value })));