簡體   English   中英

asp.net 核心自動映射器 ViewModel 到 Model

[英]asp.net core automapper ViewModel to Model

我正在按照一些示例學習實體框架的 asp.net 核心,我在保存用戶數據時遇到問題,我有從現有數據庫(數據庫優先)生成的 class,並創建了一個 ViewModel 來處理視圖,我從數據庫生成的 class 帶有一些依賴項,“ICollection”,我只需要更新表的一些字段,當我嘗試保存時出現以下錯誤:

Microsoft.EntityFrameworkCore.DbUpdateException: '更新條目時出錯。 有關詳細信息,請參閱內部異常。 SqlException: 字符串或二進制數據將被截斷。 該語句已終止。

我的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; }
    }
}

我的視圖模型:

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; }

    }
}

我將自動映射器用於 map,將 Model 用於 ViewModel,將 ViewlModel 用於 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");
}

我只想更新 ViewModel 中的字段,其他字段如密碼、圖像 url 等...我想在另一個時間單獨更新,我哪里出錯了? 這該怎么做?

謝謝!

錯誤很明顯。 只需檢查列的長度和屬性值,您就會發現一個或多個字段的長度不足以容納您要插入的數據。 例如,如果一個字段的長度為 varchar(8),而您嘗試向其中添加 10 個字符,則會出現此錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM