"arithmetic exception, numeric overflow, or string truncation string right truncation" in search using LINQ

2

I have a table in a Firebird database

CREATE TABLE CIDADE (
CID_CD              SMALLINT NOT NULL,
CID_DS              CHAR(20) NOT NULL,
CID_UF              CHAR(2) NOT NULL,
CID_DISTANCIA_SEDE  SMALLINT NOT NULL,
CID_CD_ALTERNATIVO  INTEGER NOT NULL,
CID_DT_LK           DATE NOT NULL);

I am making a query using LINQ in the columns of this table and am getting the error in columns of CHAR type:

  

arithmetic exception, numeric overflow, or string truncation   string right truncation

Identifying the problem, I noticed that the size of the fields of type String are bursting the limit of the fields. What I'm not understanding is that I'm passing exactly the limit amount of fields.

Here is my code:

  public IQueryable<Cidade> Pesquisar(Cidade cidade)
    {
        string uf = cidade.UF; // "SP" por exemplo
        var query = pctxContexto.Cidade.Where(c=> c.UF.Contains(uf));
        return query;
    }
}

Mapping:

//Mapeamento de tabela
        ToTable("CIDADE");

        //Chave primária
        HasKey(t => new { t.Codigo });

        //Propriedades
        Property(t => t.Codigo).HasColumnName("CID_CD");

        Property(t => t.Descricao)
            .IsRequired()
            .HasMaxLength(20)
            .HasColumnType("Varchar")
            .HasColumnName("CID_DS");

        Property(t => t.UF)
            .IsRequired()
            .HasMaxLength(2)
            .HasColumnType("Char")
            .HasColumnName("CID_UF");

        Property(t => t.DistanciaSede)
            .IsRequired()
            .HasColumnType("Smallint")
            .HasColumnName("CID_DISTANCIA_SEDE");

        Property(t => t.Codigo_Alternativo)
            .IsRequired()
            .HasColumnType("Int")
            .HasColumnName("CID_CD_ALTERNATIVO");

 public Cidade()
    {
        Codigo = null;
        Descricao = string.Empty;
        UF = string.Empty;
        DistanciaSede = 0;
        Codigo_Alternativo = 0;
    }

    [Key]
    public int? Codigo { get; set; }

    public string Descricao { get; set; }

    public string UF { get; set; }

    public Int16 DistanciaSede { get; set; }

    public Int32 Codigo_Alternativo { get; set; }

Could someone explain to me why this error occurred and what is the most elegant way to solve it?

    
asked by anonymous 08.01.2016 / 17:13

1 answer

1

The most elegant way to solve is by decorating attributes. Dispense to Fluent API you're using:

public class Cidade
{
    public Cidade()
    {
        Codigo = null;
        Descricao = string.Empty;
        UF = string.Empty;
        DistanciaSede = 0;
        Codigo_Alternativo = 0;
    }

    [Key]
    public int? Codigo { get; set; }
    [StringLength(20)]
    public string Descricao { get; set; }
    [StringLength(2)]
    public string UF { get; set; }

    public Int16 DistanciaSede { get; set; }

    public Int32 Codigo_Alternativo { get; set; }
}

If you use @Html.EditorFor() in Views , HTML fields are already generated with character limitation. [StringLength] also supports placing a custom error message by attribute.

EDIT

I do not know if it could be because you're using like , but I would not use contains to search by acronym:

var query = pctxContexto.Cidade.Where(c=> c.UF == uf);
    
08.01.2016 / 19:10