如何解决使用自动映射器从实体构建xml
我想使用AutoMapper从实体创建xml代码段。 与此类似:
public class SourceEntity
{
public int MasterId { get; set; }
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<SourceEntity,XElement>()
.ForMember(
dest => dest.Element("Asset.ID"),options => options.MapFrom(x => x.MasterId));
}
}
当我像这样实例化映射器时:
var config = new MapperConfiguration(
cfg => { cfg.AddProfile(new MappingProfile()); });
var mapper = config.CreateMapper();
我收到此错误:
OneTimeSetUp:AutoMapper.AutoMapperConfigurationException:仅类型的顶级单个成员支持成员的自定义配置。
我最终试图创建一个像这样的XElement:
XElement retVal = new XElement("Asset",new XElement("ID",src.MasterId)
);
.ToString()返回此:
<Asset><ID>789</ID></Asset>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。