【发布时间】:2016-05-02 19:24:49
【问题描述】:
我正在为我的 DAL 使用实体框架,并希望将实体对象转换为业务对象,反之亦然。这发生在我的 BLL 项目中。我希望在我的 BLL 项目中设置 automapper 以采用...假设由 EF 自动生成的 Customer.cs 并将其转换为 CustomerWithDifferentDetail.cs(我的业务 obj)
我尝试使用以下代码在 BLL 项目下创建 AutoMapperBLLConfig.cs:
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new CustomerProfile());
});
}
public class CustomerProfile : Profile
{
protected override void Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerWithDifferentDetail>();
cfg.CreateMap<CustomerWithDifferentDetail, Customer>();
});
}
}
然后我在 BLL 项目下创建了 CustermerService.cs 并使用以下代码测试它是否正常工作:
public void CustomerToCustomerWithDifferentDetail()
{
AutoMapperBLLConfiguration.Configure();
Customer source = new Customer
{
Account = 1234,
Purchase_Quantity = 100,
Date = "05/05/2016",
Total = 500
};
Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source)
}
我收到此错误: 缺少类型映射配置或不支持的映射。
我不确定我做错了什么。我没有 start_up 或 global.aspx。这是一个类库。我不确定我错过了什么或做错了什么。
我有一个名为 Models 的单独项目,其中包含所有业务对象,包括 CustomerWithDifferentDetail.cs。在这种情况下,CustomerWithDifferentDetail 只有两个属性:Account 和 Total。如果映射,它应该给我 Account = 1234 和 Total = 500 - 与实体对象基本相同的数据只是形状不同。
========================更新======================= ========== AutoMapperBLLConfig.cs - 与上述保持一致
CustomerProfile.cs
public class CustomerProfile : Profile
{
protected override void Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //cut it down to one line with ReverseMap
});
}
CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //missed this one line before; hence, the error
}
客户服务.cs
static CustomerService()
{
AutoMapperBLLConfiguration.Configure(); //per @Erik Philips suggestion, move this call to a static constructor
}
public void CustomerToCustomerWithDifferentDetail()
{
Customer source = new Customer
{
Account = 1234,
Purchase_Quantity = 100,
Date = "05/05/2016",
Total = 500
};
Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source);
}
结果:我的 testCustomerDTO 完全符合我的预期。
【问题讨论】:
-
创建地图后可以放.Reverse()。无需执行第二个“反转”行。
-
谢谢。我知道这一点。我想知道@James 和 Erik 是否不止一次将其称为映射...
-
使用 .ForMember 和 .Reverse 时请注意正确映射特定成员。
-
@FernandodeBem 你是什么意思小心?你能详细说明吗?我是新来的。是的,我同时使用 Reverse 和 .ForMember。提前感谢您的提醒。
标签: entity-framework automapper