gpt4 book ai didi

c# - Automapper,映射配置不持久

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:38 25 4
gpt4 key购买 nike

所以问题。

我加了

AutoMapperConfig.Configure();

到global.asax中的application_Start

它运行代码

Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelMappingProfile>();
x.AddProfile<ViewModelToDomainMappingProfile>();
});

Mapper.AssertConfigurationIsValid();

运行

public class DomainToViewModelMappingProfile : Profile
{
protected override void Configure()
{

Mapper.CreateMap<DBO.User, ViewModels.UserViewModel>();
}
}

public class ViewModelToDomainMappingProfile : Profile
{
protected override void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ViewModels.UserViewModel, DBO.User>();
});
}
}

一切都可以正常编译和运行。

但是在 Controller 中:

UserViewModel model = new UserViewModel();
User user = userService.GetUser(2);
model = Mapper.Map<User, UserViewModel>(user); //this line fails as mapping doesnt exist
return View();

但是如果我在 Controller 方法中添加映射配置

Mapper.CreateMap<ViewModels.UserViewModel,DBO.User>();
UserViewModel model = new UserViewModel();
User user = userService.GetUser(2);
model = Mapper.Map<User, UserViewModel>(user); //Works great
return View();

它工作正常。

忽略自动映射器的不同语法。我尝试了已弃用的和新的映射方式,但都失败了。

谢谢

最佳答案

问题是您在 Profile 中调用 Initialize 方法,这会导致覆盖您已经存在的映射:

public class ViewModelToDomainMappingProfile : Profile
{
protected override void Configure()
{
// you should not to call Initialize method inside your profiles.
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ViewModels.UserViewModel, DBO.User>();
});
}
}

在这里,你有两种方式:

方式 #1(使用静态 API - 已弃用)

public class DomainToViewModelMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<DBO.User, ViewModels.UserViewModel>();
}
}

public class ViewModelToDomainMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ViewModels.UserViewModel, DBO.User>();
}
}

// initialize your mapper by provided profiles
Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelMappingProfile>();
x.AddProfile<ViewModelToDomainMappingProfile>();
});

Mapper.AssertConfigurationIsValid();

方式 #2(使用实例 API)

// in this case just call CreateMap from Profile class - its the same as CreateMap on `cfg`
public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
CreateMap<DBO.User, ViewModels.UserViewModel>();
}
}

public class ViewModelToDomainMappingProfile : Profile
{
public ViewModelToDomainMappingProfile()
{
CreateMap<ViewModels.UserViewModel, DBO.User>();
}
}

// initialize you mapper config
var config = new MapperConfiguration(cfg => {
cfg.AddProfile<DomainToViewModelMappingProfile>();
cfg.AddProfile<ViewModelToDomainMappingProfile>();
});

// and then use it
var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
var dest = mapper.Map<Source, Dest>(new Source());

在方法#2 中,您需要将映射器配置存储在某处(静态字段,DI),然后在您的 Controller 中使用它。我想建议将 Mapper 实例注入(inject)到您的 Controller 中(例如,使用一些 DI 容器)。

希望对您有所帮助。

关于c# - Automapper,映射配置不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37416617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com