locked
Error when creating a record with Automapper RRS feed

  • Question

  • User1941901171 posted

    I´m usin VS 2019, .net core 3.1. I´m trying to create a crud using automapper. On the create action am getting an error, i´ve tried several forms but none seems to work

    Error:

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

    On the view

    @model ManufacturaMVC.Models.CustomerCountries

    Controller

     private readonly ApplicationDbContext _context;
            private readonly IMapper _mapper;
    
            public CustomerCountriesController(ApplicationDbContext context, IMapper mapper)
            {
                _context = context;
                _mapper = mapper;
            }
    
    public async Task<IActionResult> Create([Bind("CustomerCountry")] 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));
                }

    Error

    Missing type map configuration or unsupported mapping.
    

    Controller

    public async Task<IActionResult> Create([Bind("CustomerCountryDto")] CustomerCountriesDto 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);          
            }

    View

    @model ManufacturaMVC.ViewModels.CustomerCountriesDto
    

    Error

    The entity type 'CustomerCountriesDto' was not found
    

    View

    @model ManufacturaMVC.Models.CustomerCountries
    

    Controller

    public async Task<IActionResult> Create([Bind("CustomerCountry")] 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);          
            }

    Models

        public class CustomerCountriesDto
            {
                public string CustomerCountry { get; set; }
            }
    
    public class AutoMapping : Profile
        {
            public AutoMapping()
            {
                CreateMap<CustomerCountries, CustomerCountriesDto>(); 
            }
        }
    
     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; }
        }
    
    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; }
    
    ...
     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);
    

    Can someone please tell me how is the correct way or, what is wrong?

    Wednesday, July 8, 2020 7:59 PM

All replies