0

在ASP.NET Core 1.1 Web API中,我試圖使用AutoMapper將實體模型映射到DTO。AutoMapper將目標上的所有屬性設置爲NULL

實體模型:

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

而且在啓動設立AutoMapper的.cs:

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等)任何想法我錯了?

+0

您沒有使用AM它的設計使用的方式而這讓你陷入困境。見[這裏](https://github.com/AutoMapper/AutoMapper/issues/1570#issuecomment-235515886)。 –

回答

4

這是因爲以下行

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

是讓AM忽略所有成員的映射,包括已明確配置的人。

只需使用ForAllOtherMembers代替:

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

謝謝伊萬!這工作很好! – user1900799

相關問題