自动映射器和类层次结构

发布于 2024-11-05 20:41:17 字数 825 浏览 3 评论 0原文

给定以下源:

public class SourceBase { public string TheString { get; set; } }
public class SourceDerived : SourceBase { }

和目标:

public class DestBase { public string MyString { get; set; } }
public class DestDerived : DestBase { }

以及此映射:

  CreateMap<SourceBase, DestBase>()
    .ForMember(dest => dest.MyString, o => o.MapFrom(x => x.TheString))
    .Include<SourceDerived, DestDerived>();

  CreateMap<SourceDerived, DestDerived>();
  Mapper.AssertConfigurationIsValid();  // Exception is thrown here

但是,这会产生映射错误,指出 MyString 未映射到 DestDerived。什么给?我真的需要在所有派生类型中重复基类属性的映射吗(我的实际代码中确实有多个子类)。

编辑:

确切的例外是无法映射 DestDerived 上的以下 1 个属性:MyString。添加自定义映射表达式、忽略或重命名 DestDerived 上的属性。

Given the following sources:

public class SourceBase { public string TheString { get; set; } }
public class SourceDerived : SourceBase { }

and destinations:

public class DestBase { public string MyString { get; set; } }
public class DestDerived : DestBase { }

And this mapping:

  CreateMap<SourceBase, DestBase>()
    .ForMember(dest => dest.MyString, o => o.MapFrom(x => x.TheString))
    .Include<SourceDerived, DestDerived>();

  CreateMap<SourceDerived, DestDerived>();
  Mapper.AssertConfigurationIsValid();  // Exception is thrown here

However, this gives a mapping error saying MyString isn't mapped on DestDerived. What gives? Do I really need to repeat the mappings for base class properties in all derived types (I do have more than one subclass in my actual code).

EDIT:

The exact exception is The following 1 properties on DestDerived could not be mapped: MyString. Add a custom mapping expression, ignore, or rename the property on DestDerived.

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

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

发布评论

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

评论(1

请持续率性 2024-11-12 20:41:17

请检查这个帖子:
http://groups.google.com/group/automapper-users/browse_thread /thread/69ba514a521e9599

如果您像下面的代码一样声明它(使用 AutoMapper 1.1.0.188),它就可以正常工作。我不确定这是否能解决您的问题。

var result = Mapper.CreateMap<SourceBase, DestBase>()
                .ForMember(dest => dest.MyString, o => o.MapFrom(x => x.TheString));
               //.Include<SourceDerived, DestDerived>();
            Mapper.CreateMap<SourceDerived, DestDerived>();
            var source = new SourceDerived();
            var destDerived = new DestDerived();
            source.TheString = "teststring";
            var mapResult = Mapper.Map<SourceBase, DestBase>(source, destDerived).MyString;
            Console.WriteLine(mapResult);

Please check this post:
http://groups.google.com/group/automapper-users/browse_thread/thread/69ba514a521e9599

It works fine if you declare it like in the code below (using AutoMapper 1.1.0.188). I am not sure if this solves your problem.

var result = Mapper.CreateMap<SourceBase, DestBase>()
                .ForMember(dest => dest.MyString, o => o.MapFrom(x => x.TheString));
               //.Include<SourceDerived, DestDerived>();
            Mapper.CreateMap<SourceDerived, DestDerived>();
            var source = new SourceDerived();
            var destDerived = new DestDerived();
            source.TheString = "teststring";
            var mapResult = Mapper.Map<SourceBase, DestBase>(source, destDerived).MyString;
            Console.WriteLine(mapResult);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文