AutoMapper 在目标上将所有属性设置为 NULL

AutoMapper setting all properties to NULL on destination

在 ASP.NET Core 1.1 Web API 中,我正在尝试使用 AutoMapper 将实体模型映射到 DTO。

实体模型:

namespace InspectionsData.Models
{
    [Table("property")]
    public class Property
    {

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("property_id")]
        public int? Id { get; set; }

        [Column("show_inventory")]
        public bool ShowInventory { get; set; }

        [Column("latitude")]
        public double? Latitude { get; set; }

        [Column("longitude")]
        public double? Longitude { get; set; }

        [Column("property_type_id")]
        public int? PropertyTypeId { get; set; }
        [ForeignKey("PropertyTypeId")]        
        [Display(Name = "Property Type")]
        public PropertyType PropertyType { get; set; }

        [Column("inspection_frequency_id")]
        public int? InspectionFrequencyId { get; set; }
        [ForeignKey("InspectionFrequencyId")]        
        [Display(Name = "Inspection Frequency")]
        public InspectionFrequency InspectionFrequency { get; set; }

        [Column("group_id")]
        public int? GroupId { get; set; }
        [ForeignKey("GroupId")]
        [Display(Name = "Group")]
        public Group Group { get; set; }

        [Column("added_by_id")]
        public int? AddedById { get; set; }
        [ForeignKey("AddedById")]
        [Display(Name = "Added By")]
        public virtual User AddedBy { get; set; }

        [Column("added_date")]
        [Display(Name = "Added Date")]
        public DateTime AddedDate { get; set; }

        [Column("deleted_by_id")]
        public int? DeletedById { get; set; }
        [ForeignKey("DeletedById")]
        [Display(Name = "Deleted By")]
        public virtual User DeletedBy { get; set; }

        [Column("deleted_date")]
        [Display(Name = "Deleted Date")]
        public DateTime? DeletedDate { get; set; }
    }

和 DTO:

namespace InspectionsData.DTOs
{
    public class PropertyDto
    {
        public int? Id { get; set; }
        public bool ShowInventory { get; set; }
        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
        public PropertyType PropertyType { get; set; }
        public InspectionFrequency InspectionFrequency { get; set; }
        public DateTime NextInspectionDate { get; set; }
    }
}

映射在配置文件中完成:

public class AutoMapperProfileConfiguration : Profile
{
    public AutoMapperProfileConfiguration()
    {
        // Add as many of these lines as you need to map your objects
        var map = CreateMap<Property, PropertyDto>();
        map.ForAllMembers(opt => opt.Ignore());
        map.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
        map.ForMember(dest => dest.ShowInventory, opt => opt.MapFrom(src => src.ShowInventory));
        map.ForMember(dest => dest.Latitude, opt => opt.MapFrom(src => src.Latitude));
        map.ForMember(dest => dest.Longitude, opt => opt.MapFrom(src => src.Longitude));
        map.ForMember(dest => dest.PropertyType, opt => opt.MapFrom(src => src.PropertyType));
        map.ForMember(dest => dest.InspectionFrequency, opt => opt.MapFrom(src => src.InspectionFrequency));
    }    
}

以及Startup.cs中AutoMapper的设置:

public void ConfigureServices(IServiceCollection services)
{
    var config = new AutoMapper.MapperConfiguration(cfg =>
    {
        cfg.AddProfile(new AutoMapperProfileConfiguration());
    });

    var mapper = config.CreateMapper();
    services.AddSingleton(mapper);
}

在我的控制器操作中,我执行映射:

    [HttpGet]
    public async Task<IActionResult> GetProperty()
    {
            var properties = _context.Property
                .Include(t => t.PropertyType)
                .Include(f => f.InspectionFrequency)
                .Where(a => a.DeletedDate == null && a.GroupId == 1);


            var propertiesDto = _mapper.Map<IEnumerable<PropertyDto>>(properties);

            return Ok(propertiesDto);
        }

它没有给出错误,但是 propertiesDto 列表中所有对象的所有属性都是默认值(对象和可空类型为 NULL,布尔值为 FALSE,整数为 0 等)任何想法我走错了?

因为下面一行

map.ForAllMembers(opt => opt.Ignore());

让 AM 忽略所有成员映射,包括您明确配置的映射。

只需使用 ForAllOtherMembers 即可:

map.ForAllOtherMembers(opt => opt.Ignore());