0

我创建了一个简单的测试用例来重现我的问题。给定以下两个接口

    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 就会自动为您的属性创建映射。

从接口映射到接口时,自动映射不起作用吗?

4

1 回答 1

0

AutoMapper 需要设置器来进行映射。将这些添加到接口有助于:

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; }
}

我希望这有帮助。

于 2013-11-14T10:15:15.443 回答