AutoMapper将目标

时间:2017-07-27 11:34:38

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

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

1 个答案:

答案 0 :(得分:4)

这是因为以下一行

typedef cv::Vec<uchar, 10> Vec10b;

// ....

// Create the mat
cv::Mat_<Vec10b> some_mat;

// Access 4th channel
uchar value = some_mat.at<Vec10b>(i,j)(4); 

// or 
uchar value = some_mat.at<Vec10b>(i,j)[4];

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

只需使用typedef代替:

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