AutoMapper继承错误
AutoMapper inheritance error
我有以下摘要classes
public abstract class BaseClass
{
public int NumberProperty { get; set; }
}
public abstract class BaseClass_DTO
{
public int NumberProperty { get; set; }
public string NumberPropertyAsString { get; set; }
}
和下面具体的class
public class ConcreteA : BaseClass
{
public string StringProperty { get; set; }
}
public class ConcreteA_DTO : BaseClass_DTO
{
public string StringProperty { get; set; }
}
这是地图
Mapper.Initialize( cfg => {
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember( p => p.NumberPropertyAsString, opt => opt.MapFrom( x => x.NumberProperty.ToString() ) );
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap();
} );
Mapper.AssertConfigurationIsValid();
代码表示配置无效,因为
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ConcreteA_DTO -> ConcreteA (Source member list)
Temporary.Test.ConcreteA_DTO -> Temporary.Test.ConcreteA (Source member list)
Unmapped properties:
NumberPropertyAsString
由于 ReverseMap()
调用而发生错误。如何设置反向地图忽略成员而不复制地图?
您需要使用
忽略反向映射中的NumberPropertyAsString
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
看来在您的特定映射配置中,您应该在基础和混凝土中同时进行配置。
1.在基础class地图中。
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember(p => p.NumberPropertyAsString, opt => opt.MapFrom(x => x.NumberProperty.ToString()))
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
2。具体在class图中.
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
我有以下摘要classes
public abstract class BaseClass
{
public int NumberProperty { get; set; }
}
public abstract class BaseClass_DTO
{
public int NumberProperty { get; set; }
public string NumberPropertyAsString { get; set; }
}
和下面具体的class
public class ConcreteA : BaseClass
{
public string StringProperty { get; set; }
}
public class ConcreteA_DTO : BaseClass_DTO
{
public string StringProperty { get; set; }
}
这是地图
Mapper.Initialize( cfg => {
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember( p => p.NumberPropertyAsString, opt => opt.MapFrom( x => x.NumberProperty.ToString() ) );
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap();
} );
Mapper.AssertConfigurationIsValid();
代码表示配置无效,因为
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ConcreteA_DTO -> ConcreteA (Source member list)
Temporary.Test.ConcreteA_DTO -> Temporary.Test.ConcreteA (Source member list)
Unmapped properties:
NumberPropertyAsString
由于 ReverseMap()
调用而发生错误。如何设置反向地图忽略成员而不复制地图?
您需要使用
忽略反向映射中的NumberPropertyAsString
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
看来在您的特定映射配置中,您应该在基础和混凝土中同时进行配置。
1.在基础class地图中。
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember(p => p.NumberPropertyAsString, opt => opt.MapFrom(x => x.NumberProperty.ToString()))
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
2。具体在class图中.
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());