使用 Automapper 创建记录时出错
Error when creating a record with Automapper
我正在使用 Visual Studio 2019,.NET Core 3.1。我正在尝试使用 Autoapper 创建 CRUD。在创建操作时出现错误,我尝试了几种形式,但 none 似乎有效。
错误:
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 到 class 的映射,您有 class 到 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();
}
}
如果您通过反复试验进行编程,并不知道自己在做什么,或者只是简单地指望以正确的顺序排列几行代码……这不是正确的方法。这些迹象表明您需要通过阅读一些文档、遵循教程或回到更容易理解的地方来增加知识。如果你不这样做,你只会以一种非常低效的方式花费你自己的时间,因为你不理解你所处理的事情而变得更加沮丧。一步一步来!反复试验技术很少能带来好的结果,即使那样通常也只能这样做,它几乎从来不是解决复杂问题的好方法。
从你的问题来看,你不仅不知道应该映射什么,也不知道应该将什么添加到 DbContext 中。这些是 CRUD 的基础知识,所以我不会直接给你答案并因此伤害你,我会指导你阅读一些资料,然后你应该自己弄清楚。我知道这是问答网站,但这次我做不到。
读数:
在 ASP.NET 核心中创建 CRUD:
https://docs.microsoft.com/aspnet/core/data/ef-mvc/crud?view=aspnetcore-3.1
正在向 DbContext 添加新实体:
https://docs.microsoft.com/ef/core/saving/basic#adding-data
ASP.NET 核心中的 AutoMapper 入门:
问题 - 您是否将配置文件注册到 MapperConfiguration?喜欢:
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapping>();
});
然后您可以在您的 DI 中使用它,Ninject 下面的示例。
Bind<IMapper>().ToMethod(ctx =>
new Mapper(config, type => ctx.Kernel.Get(type)));
我正在使用 Visual Studio 2019,.NET Core 3.1。我正在尝试使用 Autoapper 创建 CRUD。在创建操作时出现错误,我尝试了几种形式,但 none 似乎有效。
错误:
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 到 class 的映射,您有 class 到 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();
}
}
如果您通过反复试验进行编程,并不知道自己在做什么,或者只是简单地指望以正确的顺序排列几行代码……这不是正确的方法。这些迹象表明您需要通过阅读一些文档、遵循教程或回到更容易理解的地方来增加知识。如果你不这样做,你只会以一种非常低效的方式花费你自己的时间,因为你不理解你所处理的事情而变得更加沮丧。一步一步来!反复试验技术很少能带来好的结果,即使那样通常也只能这样做,它几乎从来不是解决复杂问题的好方法。
从你的问题来看,你不仅不知道应该映射什么,也不知道应该将什么添加到 DbContext 中。这些是 CRUD 的基础知识,所以我不会直接给你答案并因此伤害你,我会指导你阅读一些资料,然后你应该自己弄清楚。我知道这是问答网站,但这次我做不到。
读数:
在 ASP.NET 核心中创建 CRUD:
https://docs.microsoft.com/aspnet/core/data/ef-mvc/crud?view=aspnetcore-3.1
正在向 DbContext 添加新实体:
https://docs.microsoft.com/ef/core/saving/basic#adding-data
ASP.NET 核心中的 AutoMapper 入门:
问题 - 您是否将配置文件注册到 MapperConfiguration?喜欢:
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapping>();
});
然后您可以在您的 DI 中使用它,Ninject 下面的示例。
Bind<IMapper>().ToMethod(ctx =>
new Mapper(config, type => ctx.Kernel.Get(type)));