c# - Automapper 非常简单的嵌套映射不起作用
问题描述
我试图理解为什么这个配置不映射Company
属性。它应该像这里描述的那样工作吗?
public class CreateAccountViewModel
{
public string Name { get; set; }
public string VatNumber { get; set; }
public string Acronym { get; set; }
}
public class Account
{
public string Name { get; set; }
public string VatNumber { get; set; }
public Company Company { get; set; }
}
public class Company
{
public string Acronym { get; set; }
}
public class UnitTest
{
[Fact]
public void Foo()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<CreateAccountViewModel, Account>();
cfg.CreateMap<CreateAccountViewModel, Company>();
});
config.AssertConfigurationIsValid();
}
}
测试结果:
Message:
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
=====================================================================
CreateAccountViewModel -> Account (Destination member list)
Kairos.UnitTests.CreateAccountViewModel -> Kairos.UnitTests.Account (Destination member list)
Unmapped properties:
Company
解决方案
您的案例和示例不是同一案例。在您的情况下,您需要添加自定义映射以将 Acronym 属性映射到 Company 实例。
AutoMapper
从您的示例中知道如何映射CreateAccountViewModel
到Account
和CreateAccountViewModel
到,Company
但不知道如何将字符串属性映射Acronym
到Company
.
推荐阅读
- css - 为生产版本重命名 css 样式名称
- python - 如何在DataFrame中获取最近数字的位置
- angular - 突出显示 Angular 应用程序中存在服务器端错误的响应式表单中的字段
- django - 如何使用 get_blob_to_stream 从 azure-storage 下载数据
- json - 无法使用 JsonResponse 实例序列化 Django 对象
- spring-data-jdbc - 如何加入获取关联的子关系
- list - 如何显示得分最高的“姓名”和“分数”?
- podio - Podio 领域特定的 Webhook 问题
- node.js - 如何使用 TypeORM 实体建模追随者系统
- javascript - 考虑到它们不是同质的,如何在内存中布置无类型的 javascript 数组?