Thursday, 5 September 2013

AutoMapping an Enum to an Entity Framework 5.0 complex type error

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. ContractEntity uses a Remuneration that
is a 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:
[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:
.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..

No comments:

Post a Comment