我是否需要创建automapper createmap两者兼得? [英] Do i need to create automapper createmap both ways?
问题描述
这可能是一个愚蠢的问题! (要的n00b和AutoMapper时间短!)
This might be a stupid question! (n00b to AutoMapper and time-short!)
我想使用AutoMapper从EF4实体映射到视图模型类。
I want to use AutoMapper to map from EF4 entities to ViewModel classes.
1)如果我称之为
CreateMap<ModelClass, ViewModelClass>()
然后做我还需要调用
then do I also need to call
CreateMap<ViewModelClass, ModelClass>()
执行反向?
2)如果两个类具有相同的属性名称,然后做我需要一个CreateMap声明可言,或者这只是对特定/自定义映射?
2) If two classes have the same property names, then do I need a CreateMap statement at all, or is this just for "specific/custom" mappings?
推荐答案
在AutoMapper你有一个源类型和目标类型。所以,你将只能如果你有一个相应的CreateMap这种源类型和目标类型之间的映射。因此,要回答你的问题:
In AutoMapper you have a Source type and a Destination type. So you will be able to map between this Source type and Destination type only if you have a corresponding CreateMap. So to answer your questions:
- 您不要的需要的定义反向映射。你所要做的仅仅是,如果你打算映射回。
- 是的,你需要调用CreateMap表明,这些类型是可映射的,否则,当你调用一个会抛出异常
地图&LT; TSource,TDest&GT;
告诉你一个映射源和目的地之间的类型不存在。
- You don't need to define the reverse mapping. You have to do it only if you intend to map back.
- Yes, you need to call CreateMap to indicate that those types are mappable otherwise an exception will be thrown when you call
Map<TSource, TDest>
telling you that a mapping doesn't exist between the source and destination type.
这篇关于我是否需要创建automapper createmap两者兼得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!