【发布时间】:2020-07-08 18:06:09
【问题描述】:
我正在使用 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);
}
}
有人可以告诉我正确的方法是什么,或者有什么问题吗?
【问题讨论】:
-
该错误显然与 EF 有关。您正在尝试添加一个不在您配置的任何 DbSet 中的模型。我建议您阅读 Prolog 答案中的链接。 stackoverflow.com/a/62804529/1504480
-
我很高兴它有帮助。顺便说一句,我倾向于使用 ServiceStack.Text 包,因为它不需要 AutoMapper 的所有映射设置,并且它具有许多附加功能。 github - github.com/ServiceStack/ServiceStack.Text
标签: c# .net-core controller automapper