Asp.net core mvc 自动映射将目标上的所有属性设置为NULL

Asp.net core mvc 自动映射将目标上的所有属性设置为NULL,asp.net-core-mvc,automapper,entity-framework-core,asp.net-core-webapi,asp.net-core-1.1,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("prope

在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; }
    }
而差饷物业估价署:

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));
    }    
}
在控制器操作中,我执行映射:

    [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);
        }
[HttpGet]
公共异步任务GetProperty()
{
var属性=_context.Property
.Include(t=>t.PropertyType)
.包括(f=>f.检查频率)
。其中(a=>a.DeletedDate==null&&a.GroupId==1);
var propertiesDto=\u mapper.Map(属性);
返回Ok(属性DTO);
}

它没有给出错误,但是propertiesDto列表中所有对象中的所有属性都是默认值(对象和可空类型为NULL,布尔值为FALSE,整数为0,等等)。您知道我哪里出错了吗?

这是因为下面的行

map.ForAllMembers(opt => opt.Ignore());
允许AM忽略所有成员映射,包括显式配置的映射

只需对AllotherMembers使用

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

因为下面这行

map.ForAllMembers(opt => opt.Ignore());
允许AM忽略所有成员映射,包括显式配置的映射

只需对AllotherMembers使用

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

你没有按设计的方式使用AM,这会给你带来麻烦。看,你没有按设计的方式使用AM,这会给你带来麻烦。看见