Automapper无法使用以对象为属性的抽象类

时间:2019-07-23 09:23:34

标签: c# automapper

我没有办法配置自动映射器以将其从ViewModel类映射到DTO类。 代码的结构如下:

public class BreedingD : CooperationSpotD
{
    public string ContactPerson { get; set; }
}

public abstract class CooperationSpotD 
{
    public int? Id { get; set; }
    public string Name { get; set; }
    public DateTime? CooperationStart { get; set; }
    public DateTime? CooperationEnd { get; set; }
    public bool? Active { get; set; }

    public AddressD Address { get; set; }
}

public class AddressD
{

    public int? Id { get; set; }
    public string Street { get;  set; }
    public string HouseNumber { get; set; }
    public string FlatNo { get; set; }
    public string City { get; set; }
    public string Postcode { get; set; }
}
  public class BreedingViewModel : CooperationSpotViewModel
{
    private List<int> _selectedBreeds;
    private List<BreedViewModel> _allBreeds;
    public string ContactPerson { get; set; }        

    public List<int> SelectedBreeds
    {
        get
        {
            if (_selectedBreeds == null)
                _selectedBreeds = new List<int>();
            return _selectedBreeds;
        }
        set
        {
            if (value != null)
                _selectedBreeds = value;
        }
    }

    public List<BreedViewModel> AllBreeds
    {
        get
        {
            if (_allBreeds == null)
                _allBreeds = new List<BreedViewModel>();
            return _allBreeds;
        }
        set
        {
            if (value != null)
                _allBreeds = value;
        }
    }
}

public abstract class CooperationSpotViewModel : BaseViewModel
{
    public int? Id { get; set; }
    public string Name { get; set; }
    public DateTime? CooperationStart { get; set; }
    public DateTime? CooperationEnd { get; set; }
    public bool? Active { get; set; }


    public int? AddressId { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public string FlatNo { get; set; }
    public string City { get; set; }
    public string Postcode { get; set; }
}

我要映射的类的DataProfile部分

CreateMap<BreedingViewModel, AddressD>()
            .ConstructUsing(x => new AddressD {
                City = x.City,
                FlatNo = x.FlatNo,
                HouseNumber = x.HouseNumber,
                Postcode = x.Postcode,
                Street = x.Street,
                Id = x.AddressId });

        CreateMap<BreedingViewModel, BreedingD>()
            .ConstructUsing(x => new BreedingD
            {
                Active = x.Active,
                ContactPerson = x.ContactPerson,
                CooperationStart = x.CooperationStart,
                CooperationEnd = x.CooperationEnd,
                Id = x.Id,
                Name = x.Name,

            });

当我打电话给mapper时:

var mapped = _mapper.Map<BreedingD>(vm);

我继续收到有关未映射属性地址的错误。我尝试了各种映射方式。此DataProfile是执行此操作的最新方法,但有点荒唐。

编辑:

 CreateMap<BreedingViewModel, BreedingD>()
            .ConstructUsing(x => new BreedingD
            {
                Active = x.Active,
                ContactPerson = x.ContactPerson,
                CooperationStart = x.CooperationStart,
                CooperationEnd = x.CooperationEnd,
                Id = x.Id,
                Name = x.Name,
                Address = new AddressD {  Id = x.AddressId, City = x.City, Street = x.Street, Postcode = x.Postcode, HouseNumber = x.HouseNumber, FlatNo = x.FlatNo }
            });

此DataProfile配置也不起作用。

异常消息是:

  

找到未映射的成员。在下面查看类型和成员。   添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型   对于没有匹配的构造函数,请添加一个无参数ctor,添加可选参数,或映射所有构造函数参数   AutoMapper为您创建了此类型映射,但是无法使用当前配置来映射您的类型。   BreedingViewModel-> BreedingD(目标成员列表)   SeeingEyeDog.Models.BreedingViewModel-> SeeingEyeDog.BusinessLogic.Models.BreedingD(目标成员列表)

     

未映射的属性:   地址

1 个答案:

答案 0 :(得分:0)

好,我知道了。为可能遇到这种愚蠢情况的人们发布解决方案,供将来使用。 问题不在于映射本身。我有2个DataProfile文件,一个在BusinessLogic Project中,另一个在隐藏控制器的项目中。在Startup.cs中仅注册了Business Logic项目中的一个。同时注册两个配置文件可以解决此问题。