我正在通过一些示例来学习使用Entity Framework的asp.net核心,我在保存用户数据方面遇到了问题,我有从现有数据库生成的类(数据库优先),并创建了一个ViewModel到使用视图,我的类从数据库生成了一些依赖,“ICollection”,我只需要更新表的某些字段,当我尝试保存错误发生在下面:
Microsoft.EntityFrameworkCore.DbUpdateException:'更新条目时发生错误。有关详细信息,请参阅内部异常。 SqlException:字符串或二进制数据将被截断。 声明已经终止。
我的模特:
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; }
}
}
我的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; }
}
}
我正在使用automapper将Model映射到ViewModel,将ViewlModel映射到Model。
控制器:
[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");
}
我只想更新ViewModel中的字段,其他字段如密码,图片网址等...我想在其他时间单独更新,我哪里出错了?怎么做?
谢谢!
答案 0 :(得分:1)
错误很清楚。只需检查列长度和属性值,您就会发现一个或多个字段的长度不足以容纳您尝试插入的数据。例如,如果一个字段是varchar(8)长度,并且您尝试向其中添加10个字符,则会出现此错误。