自动映射器和类层次结构
给定以下源:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请检查这个帖子:
http://groups.google.com/group/automapper-users/browse_thread /thread/69ba514a521e9599
如果您像下面的代码一样声明它(使用 AutoMapper 1.1.0.188),它就可以正常工作。我不确定这是否能解决您的问题。
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.