我创建了一个简单的测试用例来重现我的问题。 给出以下两个接口
public interface IOne
{
int Id { get; }
string PropOne { get; }
string PropTwo { get; }
string PropThree { get; }
}
public interface ITwo
{
int Id { get; }
string PropOne { get; }
string PropTwo { get; }
string PropThree { get; }
}
以下映射代码和测试
Mapper.CreateMap<IOne, ITwo>()
.ReverseMap()
;
Mapper.AssertConfigurationIsValid();
我得到了这个例外。
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
===============================================================================================================================================================================================================
ITwo -> IOne (Source member list)
Vertafore.Services.Producer.DomainModels.ApplicationService.Test.Map.DomainAutoMapperProfileTest+ITwo -> Vertafore.Services.Producer.DomainModels.ApplicationService.Test.Map.DomainAutoMapperProfileTest+IOne (Source member list)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Id
PropOne
PropTwo
PropThree
我错过了一些明显的东西吗?据我所知,CreateMap会自动为您的属性创建映射,只要它们的名称相同即可。
从接口映射到接口时自动映射不起作用吗?
答案 0 :(得分:0)
AutoMapper需要setter来进行映射。将这些添加到接口有助于:
public interface IOne
{
int Id { get; set; }
string PropOne { get; set; }
string PropTwo { get; set; }
string PropThree { get; set; }
}
public interface ITwo
{
int Id { get; set; }
string PropOne { get; set; }
string PropTwo { get; set; }
string PropThree { get; set; }
}
我希望这会有所帮助。