asp.net core automapper ViewModel to Model

asp.net-core c# entity-framework-core

Question

I'm following some examples to learn asp.net core with Entity Framework, I'm having trouble saving a user's data, I have the class that was generated from an existing database (Database First), and created a ViewModel to work with the views, my class generated from the database came with some dependencies, "ICollection", I just need to update some fields of the table, when I try to save the error occurs below:

Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.' SqlException: String or binary data would be truncated. The statement has been terminated.

My Model:

using System;
using System.Collections.Generic;

namespace PetAlerta.Domain.Entities
{
    public partial class Petshop
    {
        public Petshop()
        {
            Agendabanhos = new HashSet<Agendabanhos>();
            Agendaconsultas = new HashSet<Agendaconsultas>();
            Agendavacinas = new HashSet<Agendavacinas>();
            Banhos = new HashSet<Banhos>();
            Chat = new HashSet<Chat>();
            Configshop = new HashSet<Configshop>();
            Consultas = new HashSet<Consultas>();
            Pets = new HashSet<Pets>();
            Shopdonos = new HashSet<Shopdonos>();
        }

        public int Cod { get; set; }
        public string Razaosocial { get; set; }
        public string Nomefant { get; set; }
        public string Cnpj { get; set; }
        public string Endereco { get; set; }
        public string Cidade { get; set; }
        public string Uf { get; set; }
        public string Complemento { get; set; }
        public string Bairro { get; set; }
        public string Num { get; set; }
        public string Cep { get; set; }
        public string Endecomp { get; set; }
        public string Email { get; set; }
        public string Fone1 { get; set; }
        public string Fone2 { get; set; }
        public string Celular { get; set; }
        public string Senha { get; set; }
        public string Img { get; set; }
        public string Nomeresp { get; set; }
        public string Site { get; set; }
        public string Seguimento { get; set; }
        public DateTime? Dataacesso { get; set; }
        public int Ativo { get; set; }

        public ICollection<Agendabanhos> Agendabanhos { get; set; }
        public ICollection<Agendaconsultas> Agendaconsultas { get; set; }
        public ICollection<Agendavacinas> Agendavacinas { get; set; }
        public ICollection<Banhos> Banhos { get; set; }
        public ICollection<Chat> Chat { get; set; }
        public ICollection<Configshop> Configshop { get; set; }
        public ICollection<Consultas> Consultas { get; set; }
        public ICollection<Pets> Pets { get; set; }
        public ICollection<Shopdonos> Shopdonos { get; set; }
    }
}

My ViewModel:

using System.ComponentModel.DataAnnotations;

namespace PetAlerta.MVC.ViewModels
{
    public class PetShopViewModel
    {

        [Key]
        public int Cod { get; set; }
        public string Razaosocial { get; set; }
        public string Nomefant { get; set; }
        public string Cnpj { get; set; }
        public string Endereco { get; set; }
        public string Cidade { get; set; }
        public string Uf { get; set; }
        public string Complemento { get; set; }
        public string Bairro { get; set; }
        public string Num { get; set; }
        public string Cep { get; set; }
        public string Endecomp { get; set; }
        public string Email { get; set; }
        public string Fone1 { get; set; }
        public string Fone2 { get; set; }
        public string Celular { get; set; }
        public string Nomeresp { get; set; }
        public string Site { get; set; }

    }
}

I'm using the automapper to map the Model to the ViewModel and the ViewlModel to the Model.

Controller:

[HttpPost]
public IActionResult SalvaPerfil([FromBody] PetShopViewModel petshopViewModel)
{
    if (ModelState.IsValid)
    {
        var petshop = Mapper.Map<PetShopViewModel, Petshop>(petshopViewModel);

        _PetApp.Update(petshop);

        return Content("fim..."); //<<=== ERROR
    }

    return Content("ERRO");
}

I just want to update the fields that is in the ViewModel, other fields like password, image url etc ... I want to update separately at another time, where am I going wrong? how to do this?

Thanks!

1
2
6/14/2018 6:12:19 PM

Accepted Answer

Error is very clear. Just check the columns length and properties value, you'll find that the length of one or more fields is NOT big enough to hold the data you are trying to insert. For example, if one field is a varchar(8) length, and you try to add 10 characters in to it, you will get this error.

1
6/14/2018 6:13:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow