我一直在使用EF Core (2.2)
和Automapper
。我有一些对象:两个实体(Partner,PartnerStatus)和集成模型(IntegrationPartnerModel)。
我需要将两个对象(Partner,IntegrationPartnerModel)映射到不同属性上的一个对象(PartnerStatus)。
下面是它们各自的定义:
public class Partner {
public int PartnerId { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
// Relation Partner and PartnerStatus
public int PartnerStatusId { get; set; }
// Navigation property to PartnerStatus
public PartnerStatus PartnerStatus { get; set; }
// Relation IntegrationPartnerModel and PartnerStatus
public string PartnerStatusCode { get; set; }
}
public class PartnerStatus {
public int Id { get; set; }
public string Name { get; set; }
// Status code from integration service
public string Code { get; set; }
}
public class IntegrationPartnerModel {
public string Name { get; set; }
// relation to PartnerStatus entity (Code property)
public string PartnerStatusCode { get; set; }
// Navigation property to PartnerStatus entity
public PartnerStatus PartnerStatus { get; set; }
// etc
}
我已经制作了两个Automapper的配置文件:
public class PartnerStatusProfile : Profile {
public PartnerStatusProfile()
{
CreateMap<PartnerStatus, Partner>()
.ForMember(p => p.PartnerStatusId, o => o.MapFrom(s => s.Id))
.ReverseMap();
CreateMap<PartnerStatus, IntegrationModel>()
.ForMember(p => p.PartnerStatusCode, o => o.MapFrom(s => s.Code))
.ReverseMap();
}
}
public class PartnerProfile : Profile
{
public PartnerProfile() {
CreateMap<IntegrationPartnerModel, Partner>()
.IncludeMembers(integration => integration.PartnerStatus)
.ForMember(p => p.PartnerStatusCode, o => o.MapFrom(p => p.PartnerStatusCode))
}
}
映射Partner和PartnerStatus(PartnerStatusId为关系)成功。
但是IntegrationPartnerModel和Partner(PartnerStatusCode是关系)的映射失败。
var integrationPartnerModel = new IntegrationPartnerModel {
PartnerStatusCode = "ABC"
//...
};
var newPartner = _mapper.Map<IntegrationPartnerModel, Partner>(integrationPartnerModel);
属性newPartner.PartnerStatusCode =“”(默认值),并且newPartner.PartnerStatus为空。
我在做什么错了?