我需要双向创建 automapper createmap 吗? [英] Do i need to create automapper createmap both ways?

查看:35
本文介绍了我需要双向创建 automapper createmap 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题!(n00b 到 AutoMapper 并且时间很短!)

This might be a stupid question! (n00b to AutoMapper and time-short!)

我想使用 AutoMapper 将 EF4 实体映射到 ViewModel 类.

I want to use AutoMapper to map from EF4 entities to ViewModel classes.

1) 如果我打电话

CreateMap<ModelClass, ViewModelClass>()

那我还需要打电话吗

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 中,您有一个 Source 类型和一个 Destination 类型.因此,只有当您有相应的 CreateMap 时,您才能在此 Source 类型和 Destination 类型之间进行映射.所以回答你的问题:

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:

  1. 您不需要定义反向映射.仅当您打算映射回来时才必须这样做.
  2. 是的,您需要调用 CreateMap 来指示这些类型是可映射的,否则当您调用 Map 时会抛出异常,告诉您这些类型之间不存在映射源和目标类型.
  1. You don't need to define the reverse mapping. You have to do it only if you intend to map back.
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆