2

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!

2
  • 1
    Your error says it all, one of your model values is bigger than the size of the column in the database you are trying to save it to, hence "the value would be truncated." To fix this, find the column in your database which is too small for the value and change it to accept bigger amounts of data, so if it was varchar(100) increase it to varchar(255) or varchar(MAX) etc. Jun 14, 2018 at 18:08
  • You probably have a huge text with a small column, I guess thats not your issue... Jun 14, 2018 at 18:08

1 Answer 1

1

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
  • I used the "Scaffold-DbContext -DataAnnotations" command now with the field details, and I adjusted the forms, the mentioned error no longer occurs. But still I do not know how to update only the fields that are in the ViewModel, the fields not sent in POST are getting as NULL in the database, should I ask that question in another post? thanks!
    – MariINova
    Jun 14, 2018 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.