代码之家  ›  专栏  ›  技术社区  ›  JED

AutoMapper ASP.NET Core 2:未映射的属性

  •  0
  • JED  · 技术社区  · 6 年前

    当试图将我的dto映射到我的模型时,我得到一个错误,即某些属性没有被映射。

    未映射的属性:
    身份证件
    地址ID
    联系人ID
    谷仓
    网关
    用户场


    但是id应该是自动生成的,addressid、contactid和barns是可以为空的属性。

    问题发生在我到达 var farmToCreate = _mapper.Map<Farm>(farmForCreateDto); 在控制器内。 它试图记录运行 SaveChangesAsync 在数据库上,并在此时引发一个错误,即由于未映射的属性,它无法将场DTO映射到其模型。


    农场管制员

    [ServiceFilter(typeof(LogUserActivity))]
    [Authorize]
    [Route("api/[controller]")]
    public class FarmsController : Controller
    {
        private readonly IBaseRepository _repo;
        private readonly IMapper _mapper;
    
        public FarmsController(IBaseRepository repo, IMapper mapper)
        {
            _mapper = mapper;
            _repo = repo;
        }
    
        [HttpPost("createFarm")]
        public async Task<IActionResult> CreateFarm([FromBody] FarmForCreateDto farmForCreateDto)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);
    
            Address createdFarmAddress = null;
    
            if (farmForCreateDto.Address != null)
            {
                var farmAddressToCreate = _mapper.Map<Address>(farmForCreateDto.Address);
    
                createdFarmAddress = await _repo.CreateAddress(farmAddressToCreate);
            }
    
            Address createdFarmContactAddress = null;
    
            if (farmForCreateDto.Contact.Address != null)
            {
                var farmContactAddressToCreate = _mapper.Map<Address>(farmForCreateDto.Contact.Address);
    
                createdFarmContactAddress = await _repo.CreateAddress(farmContactAddressToCreate);
            }
    
            Contact createdFarmContact = null;
    
            if (farmForCreateDto.Contact != null)
            {
                var farmContactToCreate = _mapper.Map<Contact>(farmForCreateDto.Contact);
    
                farmContactToCreate.Address = createdFarmContactAddress;
    
                createdFarmContact = await _repo.CreateContact(farmContactToCreate);
            }
    
            var farmToCreate = _mapper.Map<Farm>(farmForCreateDto);
    
            farmToCreate.Address = createdFarmAddress;
            farmToCreate.Contact = createdFarmContact;
    
            var createdFarm = await _repo.CreateFarm(farmToCreate);
    
            var farmToReturn = _mapper.Map<FarmForDetailedDto>(createdFarm);
    
            return CreatedAtRoute("GetFarm", new { controller = "Farms", id = createdFarm.Id }, farmToReturn);
        }
    


    模型\场.cs

    public class Farm
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    
        public int? AccountId { get; set; }
        public Account Account { get; set; }
    
        public int? AddressId { get; set; }
        public Address Address { get; set; }
    
        public int? ContactId { get; set; }
        public Contact Contact { get; set; }
    
        public ICollection<Barn> Barns { get; set; }
    }
    


    dto\farmforcreatedto.cs

    public class FarmForCreateDto
    {
        [Required]
        public string Name { get; set; }
    
        public Models.Account Account { get; set; }
    
        public ContactForCreateDto Contact { get; set; }
    
        public AddressForCreateDto Address { get; set; }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Antonio Campagnaro    6 年前

    您需要配置映射。

    How to configure mappings