【问题标题】:AutoMapper setting all properties to NULL on destinationAutoMapper 将目标上的所有属性设置为 NULL
【发布时间】:2018-01-03 02:47:39
【问题描述】:

在 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 等)。我哪里出错了?

【问题讨论】:

  • 您没有按照设计使用的方式使用 AM,这会给您带来麻烦。见here

标签: asp.net-core-mvc automapper entity-framework-core asp.net-core-webapi asp.net-core-1.1


【解决方案1】:

因为下面一行

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

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

只需改用ForAllOtherMembers

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2019-04-18
    • 2012-09-12
    相关资源
    最近更新 更多