首页 > 解决方案 > 使用 Automapper 创建记录时出错

问题描述

我正在使用 Visual Studio 2019、.NET Core 3.1。我正在尝试使用 Autoapper 创建一个 CRUD。在创建操作时出现错误,我尝试了几种形式,但似乎都没有。

错误:

The entity type 'CustomerCountriesDto' was not found. Ensure that the entity type has been added to the model.

在观点上:

@model ManufacturaMVC.Models.CustomerCountries

控制器:

private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
    
public CustomerCountriesController(ApplicationDbContext context, IMapper mapper)
{
    _context = context;
    _mapper = mapper;
}

public async Task<IActionResult> Create([Bind("CustomerCountries")] CustomerCountries customerCountries)
{
    if (ModelState.IsValid)
    {
        //var model = _mapper.Map<CustomerCountriesDto>(customerCountries);
        var user = _mapper.Map<CustomerCountries, CustomerCountriesDto>(customerCountries);
        _context.Add(user);
        await _context.SaveChangesAsync();                
        return RedirectToAction(nameof(Index));
    }
}

错误:

Missing type map configuration or unsupported mapping.

控制器:

public async Task<IActionResult> Create([Bind("CustomerCountriesDto")] CustomerCountries customerCountries)
{
    if (ModelState.IsValid)
    {
        var model = _mapper.Map<CustomerCountriesDto>(customerCountries);
        var user = _mapper.Map<CustomerCountriesDto, CustomerCountries>(customerCountries);
        _context.Add(user);
        await _context.SaveChangesAsync();                
        return RedirectToAction(nameof(Index));
    }
    return View(customerCountries);          
}

看法:

@model ManufacturaMVC.ViewModels.CustomerCountriesDto

错误:

The entity type 'CustomerCountriesDto' was not found

看法:

@model ManufacturaMVC.Models.CustomerCountries

控制器:

    public async Task<IActionResult> Create([Bind("CustomerCountriesDto")] CustomerCountries customerCountries)
{
    if (ModelState.IsValid)
    {
        var model = _mapper.Map<CustomerCountriesDto>(customerCountries);
        //var user = _mapper.Map<CustomerCountries, CustomerCountriesDto>(customerCountries);
        _context.Add(model);
        await _context.SaveChangesAsync();                
        return RedirectToAction(nameof(Index));
    }
    return View(customerCountries);          
}

楷模:

public class CustomerCountries
{
    [StringLength(50, ErrorMessage = "Longitud máxima para el país: 50")]
    public string CustomerCountry { get; set; }
    
    public ICollection<CustomerRegions> CustomerRegions { get; set; }
}

DTO:

public class CustomerCountriesDto
{
    public string CustomerCountry { get; set; }
}

映射配置文件:

public class AutoMapping : Profile
{
    public AutoMapping()
    {
        CreateMap<CustomerCountries, CustomerCountriesDto>(); 
    }
}

数据库上下文:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    
    }
    
    public DbSet<Categories> Categories { get; set; }
    public DbSet<CustomerCities> CustomerCities { get; set; }
    public DbSet<CustomerCountries> CustomerCountries { get; set; }
    
    // More code ...

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Categories>().HasKey(m => m.CategoryId);
        modelBuilder.Entity<CustomerCities>().HasKey(m => m.CustomerCity);
        modelBuilder.Entity<CustomerCountries>().HasKey(m => m.CustomerCountry);
    }
}

有人可以告诉我正确的方法是什么,或者有什么问题吗?

标签: c#.net-corecontrollerautomapper

解决方案


我认为你缺少 Dto 到类的映射,你有类到 Dto。

public class AutoMapping : Profile
{
    public AutoMapping()
    {
        CreateMap<CustomerCountries, CustomerCountriesDto>(); 
        //add this
        CreateMap<CustomerCountriesDto, CustomerCountries>();
    }
}

编辑:还发现您可以添加.ReverseMap()到映射中。那应该反过来覆盖映射。

public class AutoMapping : Profile
{
    public AutoMapping()
    {
        CreateMap<CustomerCountries, CustomerCountriesDto>().ReverseMap();       
    }
}

推荐阅读