c# - 使用 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);
}
}
有人可以告诉我正确的方法是什么,或者有什么问题吗?
解决方案
我认为你缺少 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();
}
}
推荐阅读
- typo3 - 我如何更改 Typo3 中的 Favicon
- angular - 我什么时候使用 [value] 和/或 [ngValue] 或者什么时候不使用?
- excel - 有没有办法在循环中每次使用不同的预定义数组
- javascript - 使用 JavaScript 中的 FileSaver.js 从 FileName 中删除当前时间戳
- git - git logpickup(show) 提交是否仅与某些分支或标签相关联?
- angular - 如何在 ngx-cookieconsent 中为 Angular 手动设置 cookie 状态
- c++ - 读取 mifare 卡块并将其块的一部分与一些文本进行比较
- java - 删除 ListView 项目 - Android
- python - Keras LSTM input_shape 的问题:预期 lstm_1_input 的形状为 (500, 2) 但得到的数组的形状为 (500, 5)
- java - 如何在JComboBox中显示信息,但后台操作选择不同的信息