【发布时间】:2018-06-15 02:13:37
【问题描述】:
我正在按照一些示例来学习带有实体框架的 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; }
}
}
我的视图模型:
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; }
}
}
我正在使用自动映射器将模型映射到 ViewModel,并将 ViewlModel 映射到模型。
控制器:
[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 中的字段,其他字段,如密码、图像 url 等……我想在其他时间单独更新,我哪里出错了?如何做到这一点?
谢谢!
【问题讨论】:
-
您的错误说明了一切,您的模型值之一大于您尝试将其保存到的数据库中列的大小,因此“该值将被截断。”要解决此问题,请在数据库中找到该值太小的列并将其更改为接受更大数量的数据,因此如果它是 varchar(100),请将其增加到 varchar(255) 或 varchar(MAX) 等。
-
您可能有一个带有小列的大文本,我想这不是您的问题...
标签: c# asp.net-core entity-framework-core