简体   繁体   English

自动将枚举映射到Entity Framework 5.0复杂类型错误

[英]AutoMapping an Enum to an Entity Framework 5.0 complex type error

So the error is this: Expression must resolve to top-level member and not any child object's properties. 所以错误是这样的:表达式必须解析为顶级成员,而不是任何子对象的属性。

Remuneration in the DTO is a Enum. DTO的薪酬为枚举。 ContractEntity uses a Remuneration that is a ComplexType. ContractEntity使用的报酬是ComplexType。

The code throwing the error: 引发错误的代码:

Mapper.CreateMap<ContractDTO, ContractEntity>()
            .ForMember(d => d.Remuneration.ContractType, s => s.MapFrom(z => z.ContractType))
            .ForMember(d => d.Remuneration.Currency, s => s.MapFrom(z => z.Currency))
            .ForMember(d => d.Remuneration.RateUnit, s => s.MapFrom(z => z.RateUnit));

Entity Framework complex type: 实体框架复杂类型:

 [ComplexType]
public class Remuneration
{
    public decimal Amount { get; set; }
    public int Currency { get; set; }
    public int RateUnit { get; set; }
    public int ContractType { get; set; }    
}

Because I want the destination (ContractEntity) to use the integer values, I thought I could just cast the source enum to the destination integer like this: 因为我希望目标(ContractEntity)使用整数值,所以我认为可以将源枚举强制转换为目标整数,如下所示:

.ForMember(d => d.Remuneration.ContractType, s => s.MapFrom(z => (int)z.ContractType))

.. obviously I cant, and was hoping that someone could clarify why this doesn't work.. ..显然我不能,并且希望有人可以弄清为什么这不起作用。

If you have problmes with complex type mapping, you can implement manual mapping based on AutoMapper: 如果您遇到复杂类型映射的问题,则可以基于AutoMapper实施手动映射:

Mapper.CreateMap<PatternDto, PatternModel>().ConvertUsing(pattern =>
                {
                    if(pattern == null) return new PatternModel();
                    return new PatternModel
                        {
                            EmailPattern = pattern.EmailPattern,
                            SmsPattern = pattern.SmsPattern
                        };
                });

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM