Automapper 非常简单的嵌套映射不起作用
Automapper very simple nested mapping doesn't work
我想了解为什么此配置不映射 Company
属性。它应该像描述的那样工作吗here?
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
.
AutoMapper 无法推断 Acronym
应该从 属性 映射到 Company
属性 引用的对象上。通过将目标 属性 重命名为 CompanyAcronym
,AutoMapper 应该能够 link 将两者结合在一起,如 here 所述。
我想了解为什么此配置不映射 Company
属性。它应该像描述的那样工作吗here?
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
.
AutoMapper 无法推断 Acronym
应该从 属性 映射到 Company
属性 引用的对象上。通过将目标 属性 重命名为 CompanyAcronym
,AutoMapper 应该能够 link 将两者结合在一起,如 here 所述。