Я использую Visual Studio 2019, NET Core 3.1. Я пытаюсь создать CRUD с помощью Autoapper. При создании действия я получаю сообщение об ошибке. Я пробовал несколько форм, но ни одна из них не работает.
Ошибка:
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>();
}
}
DbContext:
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);
}
}
Может кто-нибудь подскажите пожалуйста, как правильно или что не так?