- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下模型:
public class AccountDto
{
public Guid Id { get; set; }
public string ChamberOfCommerce { get; set; }
public string Code { get; set; }
public VatDto PurchaseVatCode { get; set; }
public VatDto SalesVatCode { get; set; }
}
public class VatDto
{
public string Code { get; set; }
public string Description { get; set; }
public double Percentage { get; set; }
}
public class Account
{
public Guid Id { get; set; }
public string ChamberOfCommerce { get; set; }
public string Code { get; set; }
public string PurchaseVatCode { get; set; }
public string PurchaseVatDescription { get; set; }
public double PurchaseVatPercentage { get; set; }
public string SalesVatCode { get; set; }
public string SalesVatDescription { get; set; }
public double SalesVatPercentage { get; set; }
}
我想以平面模式读取数据,然后将其映射到 AccountDto
。我尝试使用以下行配置 AutoMapper:
var config = new MapperConfiguration(cfg => {
cfg.AddMemberConfiguration()
.AddMember<NameSplitMember>()
.AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Purchase", "Sales"))
.AddName<ReplaceName>(_ => _.AddReplace("Purchase", string.Empty).AddReplace("Sales", string.Empty));
cfg.RecognizePrefixes("Purchase");
cfg.RecognizePrefixes("Sales");
cfg.RecognizeDestinationPrefixes("Purchase");
cfg.RecognizeDestinationPrefixes("Sales");
cfg.CreateMap<AccountDto, Models.Account>().ReverseMap();
});
var mapper = config.CreateMapper();
var dto = mapper.Map<AccountDto>(account);
但以上尝试均无效,我仍然无法映射 PurchaseVatCode
。
我收到以下错误:
Property:
PurchaseVatCode ---> AutoMapper.AutoMapperConfigurationException:
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
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
String -> VatDto (Destination member list)
System.String -> Online.Account.Domain.Dto.VatDto (Destination member list)
Unmapped properties:
Code
Description
TranslatedDescription
Percentage
谁能帮忙解决这个问题?提前致谢!
最佳答案
所有的困难都源于 AccountDto
属性名称 PurchaseVatCode
和 SalesVatCode
的错误选择。为了让 AutoMapper 正确地拼合名称,我们需要去掉 Code
后缀。
问题是 Account
具有名称相同但类型不同的属性。所以我们不能使用 RecognizeAlias
来重命名它们,因为它也会重命名目标属性。
源和目标唯一不同的方法是指定前缀/后缀。但是,如果我们使用 RecognizeSuffix("Code")
从源代码 PurchaseVatCode
和 SalesVatCode
中去除 Code
,那不幸的是,它也影响了 VatDto.Code
属性(这可能被认为是当前的 AutoMapper 缺陷)。
在这种情况下,您可以做的最好的事情是使用后者来完成大部分工作并显式映射 Code
属性:
var config = new MapperConfiguration(cfg =>
{
cfg.RecognizePostfixes("Code");
cfg.CreateMap<AccountDto, Account>()
.ForMember(dst => dst.PurchaseVatCode, opt => opt.MapFrom(src => src.PurchaseVatCode.Code))
.ForMember(dst => dst.SalesVatCode, opt => opt.MapFrom(src => src.SalesVatCode.Code))
.ReverseMap();
});
关于c# - AutoMapper 定制 - 将字符串映射到 DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691353/
我目前被指派去调查并以某种方式找到一种“定制”(对身份验证、常量、消息等进行修改)OpenSSH 的方法,并且作为一个“基于网络”的人,我真的不知道从哪儿开始。因此非常感谢评论。 问题: 我从 her
我拥有的 excel 文件超过 1,000,000 行和 26 列。 下面是用于查找特定数据的代码,并根据该数据创建一个新文件,目前创建一个新文件大约需要 15 分钟 请如果有专家可以帮助我更快地处理
我正在处理Zend_Form现在,我很难弄清楚如何: 使用表单按钮的自定义图像, 在特定位置插入文本和链接(就我而言,我想在提交按钮之前添加“忘记密码?”链接)。 我已通读手册,但没有看到任何相关内容
是否可以将图像添加到 UISwitch 背景,例如当状态为 ON 时(作为一个背景)和当状态为 OFF 时(另一个背景图像)? 最佳答案 要更改背景颜色(不是图像),您只需执行以下操作即可。这会更改领
到目前为止,我刚刚开始使用 Octave 并在我的终端上运行它。 每次打开提示符时,我的命令行都以: octave-3.4.0:1> 因此我使用以下内容来使其更短且更易于阅读: PS1('>> ')
在阅读Struts2文档时,我遇到了下面引用的段落 customizing controller - Struts 1 lets to customize the request processor
我正在尝试自定义 jQuery Tag-It 小部件 (http://aehlke.github.com/tag-it/) 以实现以下两种行为: 1)允许在标签中使用逗号(我可以通过自定义trigge
我是整个 Emacs 的新手,让我着迷的一件事是开箱即用的 Emacs 在编程时不会让您陷入困境。我主要使用 Python 和 C++ 进行编程,然后按回车键将光标发送回新行的第 1 列,而不是让你停
我有这些行 y DB,我想按以下顺序排序,并包含字符和数字。 Score 列是一个 varchar。获胜者和失败者也在分数栏中。 得分 WINNER 100+ 100 90 80+ 80 50 LOS
我正在使用 Bootstrap,您如何自定义轮播? 有什么建议吗? https://v4-alpha.getbootstrap.com/components/carousel/ 最佳答案 .activ
我有一个投票设置,使用脚本将其拉入我的 WP 页面。通过http://quipol.com/ EG 我已经通过 firebug 找到了样式并在其中相应地自定义了它们,但我想知道是否有一种方法可以实现
美好的一天。 如果 JLabel 和 JTextField 字体大小可以根据需要更改,是否也可以更改 JTable 的列名称和元素的字体样式(大小、外观、颜色)? 添加更多内容,我正在使用 Windo
进一步回答我的问题Java JFilechooser 。建议扩展 BasicFileChooserUI,重写 create/getModel 并提供 BasicDirectoryModel 的实现。
我想制作(好吧..正在制作..)一个标签页。我用 border-top:none 属性制作了一个“选项卡框”,所以它看起来像是选项卡的一部分,在里面我有一个表格。 我想知道,有没有办法删除表格标题的所
我有大量的项目正在进行中,还有几个解决方案(它们是项目“池”的子集)。有时拥有一个仅用于特定测试的 .sln 是件好事。 问题: NUGet 分别绑定(bind)到每个解决方案。 NUGet 喜欢在
我计划编写一些 git 钩子(Hook)作为一个项目,将用户的操作记录在数据库中。然后可以使用该数据库查询他的所有事件。我尝试记录的操作是 promise pull 推送 merge 分支机构 我想把
大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享学习心得,希望我的文章能成为你成长路上的垫脚石,让我们一起精进。 在本文中,我们将学习中间件,以及如何使用它进一步定制应用程序。我
我正在尝试使用 yasg 自定义我的 api 文档。 首先,我想确定我自己的部分的命名,以及本部分应包含哪些端点。似乎部分的命名是基于不属于最长公共(public)前缀的第一个前缀,例如: 如果我们有
我需要(即客户要求)提供自定义键盘,供用户在文本字段和区域中输入文本。我已经有一些可以执行键盘操作并将测试附加到文本字段的东西,但是我想让它更通用并让它像标准的 iphone 键盘一样工作,即当用户选
我有一个项目,它在特定位置(不是/src/resources)包含资源(模板文件)。我希望在运行 package-bin 时将这些资源打包。 我看到了 package-options 和 packag
我是一名优秀的程序员,十分优秀!