我需要双向创建 automapper createmap 吗?

发布于 2024-11-10 14:37:49 字数 362 浏览 0 评论 0原文

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

我想使用 AutoMapper 从 EF4 实体映射到 ViewModel 类。

1)如果我打电话

CreateMap<ModelClass, ViewModelClass>()

,那么我还需要打电话

CreateMap<ViewModelClass, ModelClass>()

来执行相反的操作吗?

2)如果两个类具有相同的属性名称,那么我是否需要 CreateMap 语句,或者这只是用于“特定/自定义”映射?

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

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

1) If I call

CreateMap<ModelClass, ViewModelClass>()

then do I also need to call

CreateMap<ViewModelClass, ModelClass>()

to perform the reverse?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗喜 2024-11-17 14:37:49

为偶然发现这个问题的人提供信息。现在似乎有一种内置方法可以通过在 CreateMap() 配置链末尾添加 .ReverseMap() 调用来实现反向映射。

For the info of the people who stumble upon this question. There appears to be now a built-in way to achieve a reverse mapping by adding a .ReverseMap() call at the end of your CreateMap() configuration chain.

俏︾媚 2024-11-17 14:37:49

在 AutoMapper 中,您有一个源类型和一个目标类型。因此,仅当您有相应的 CreateMap 时,您才能够在该 Source 类型和 Destination 类型之间进行映射。因此,回答您的问题:

  1. 您不需要定义反向映射。仅当您打算向后映射时才必须执行此操作。
  2. 是的,您需要调用 CreateMap 来指示这些类型是可映射的,否则当您调用 Map 时会抛出异常,告诉您源和目标之间不存在映射类型。

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. 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.
落墨 2024-11-17 14:37:49

我使用了扩展方法来映射两种

    public static IMappingExpression<TDestination, TSource> BothWays<TSource, TDestination>
        (this IMappingExpression<TSource, TDestination> mappingExpression)
    {
        return Mapper.CreateMap<TDestination, TSource>();
    }

用法:

 CreateMap<Source, Dest>().BothWays();

I've used an extension method do mapping both ways

    public static IMappingExpression<TDestination, TSource> BothWays<TSource, TDestination>
        (this IMappingExpression<TSource, TDestination> mappingExpression)
    {
        return Mapper.CreateMap<TDestination, TSource>();
    }

usage:

 CreateMap<Source, Dest>().BothWays();
老子叫无熙 2024-11-17 14:37:49
  1. 是的,或者您可以调用 CreateMap().ReverseMap()
  2. 如果两个类具有相同的Member(Property,Field,GetMethod()),则无需调用CreateMap。实际上,如果TDest中的每个member都存在于TSrc中,则不需要调用CreateMapCreateMap代码>.以下代码有效。
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }  
}
class Person2
{
   public string Name { get; set; }
   public int? Age { get; set; }
   public DateTime BirthTime { get; set; }
}
public class NormalProfile : Profile
{
    public NormalProfile()
    {
       //CreateMap<Person2, Person>();//
    }
}
   
var cfg = new MapperConfiguration(c => 
{ 
    c.AddProfile<NormalProfile>();
});
//cfg.AssertConfigurationIsValid();
var mapper = cfg.CreateMapper();
var s3 = mapper.Map<Person>(new Person2 { Name = "Person2" });
  1. Yes, or you can call CreateMap<ModelClass, ViewModelClass>().ReverseMap().
  2. If two classes have same Member(Property,Field,GetMethod()), you needn't call CreateMap<TSrc,TDest>. Actually, if every member in TDest are all exist in TSrc, you needn't call CreateMap<TSrc,TDest>. The following code works.
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }  
}
class Person2
{
   public string Name { get; set; }
   public int? Age { get; set; }
   public DateTime BirthTime { get; set; }
}
public class NormalProfile : Profile
{
    public NormalProfile()
    {
       //CreateMap<Person2, Person>();//
    }
}
   
var cfg = new MapperConfiguration(c => 
{ 
    c.AddProfile<NormalProfile>();
});
//cfg.AssertConfigurationIsValid();
var mapper = cfg.CreateMapper();
var s3 = mapper.Map<Person>(new Person2 { Name = "Person2" });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文