AutoMapper.Mapper.CreateMap&LT; TSource,TDestination&GT;()“已过时 [英] AutoMapper.Mapper.CreateMap<TSource,TDestination>()' is obsolete
问题描述
我不得不类一样。
class A
{
public int id {get; set;}
}
class B
{
public C c {get; set;}
}
class C
{
public int id {get; set;}
public string Name {get; set;}
}
我的要求是A类的ID映射到C类的ID
现在我在做什么到现在是:
。Mapper.CreateMap()ForMember(DES => des.C.Id,SRC => src.MapFrom(X => x.id));
My requirement is to map id of class A to id of class C. Now what I was doing till now was: Mapper.CreateMap().ForMember(des => des.C.Id, src => src.MapFrom(x => x.id));
和它工作正常。
现在好像自动映射器改变了他们的执行。而我得到的警告如下:
Now seems like Auto mapper has changed their implementation. and I am getting warning as below:
AutoMapper.Mapper.CreateMap()已过时:动态创建的地图将在5.0版本中移除。使用MapperConfiguration实例,并根据需要,或Mapper.Initialize静态存储。使用CreateMapper创建一个映射器的实例。
AutoMapper.Mapper.CreateMap()' is obsolete: 'Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.
我要地图有不同的名称和结构类的一些属性。任何帮助。
I need to map some properties of classes which has different name and structure. Any help on this.
推荐答案
previously
Previously
Mapper.CreateMap<Src, Dest>()
.ForMember(d => d.UserName, opt => opt.MapFrom(/* ????? */));
这里的问题是映射定义是静态的,定义一次,整个应用程序的生命周期重复使用。 3.3之前,你可能需要重新定义映射每个请求,用硬codeD值。而且,由于映射配置是不是我们的映射执行一个独立的位置创建,我们需要一些方法来在我们的配置引入了运行参数,然后执行期间提供的。
The problem here is mapping definitions are static, defined once and reused throughout the lifetime of the application. Before 3.3, you would need to re-define the mapping on every request, with the hard-coded value. And since the mapping configuration is created in a separate location than our mapping execution, we need some way to introduce a runtime parameter in our configuration, then supply it during execution.
这两个部分就完成了。要与运行参数创建映射定义,打假闭包,其中包括一个名为局部变量:
This is accomplished in two parts: the mapping definition where we create a runtime parameter, then at execution time when we supply it. To create the mapping definition with a runtime parameter, we "fake" a closure that includes a named local variable:
Mapper.Initialize(cfg => {
string userName = null;
cfg.CreateMap<Source, Dest>()
.ForMember(d => d.UserName,
opt => opt.MapFrom(src => userName)
);
});
有关详细信息,<一个href=\"https://lostechies.com/jimmybogard/2014/12/30/automapper-3-3-feature-parameterized-projections/#gist17754179\"相对=nofollow>看到这个
有关的一个或多个类
cfg.CreateMissingTypeMaps = true;
cfg.CreateMap<Source, Dest>()
.ForMember(d => d.UserName,
opt => opt.MapFrom(src => userName)
);
cfg.CreateMap<AbcEditViewModel, Abc>();
cfg.CreateMap<Abc, AbcEditViewModel>();
});
在映射类
IMapper mapper = config.CreateMapper();
var source = new AbcEditViewModel();
var dest = mapper.Map<AbcEditViewModel, Abct>(source);
这篇关于AutoMapper.Mapper.CreateMap&LT; TSource,TDestination&GT;()“已过时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!