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 I am getting the error in the columns of type CHAR:
arithmetic Exception, Numeric overflow, or string truncation string right truncation
Identifying the problem, I realized that the size of the fields of the String type are breaking the limit of the fields. What I don’t understand is that I’m passing exactly limit amount of fields.
Just follow 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 mistake and what is the most elegant way to solve?
Post your class
Cidade
, please.– Thiago Lunardi
I edited the question by adding the city class
– Paulo Henrique
Post also your class
Codigo
, please.– Thiago Lunardi
Your class
Cidade
has a fieldKey
that isnullable
? This can’t be right.– Thiago Lunardi
Code is not class is an integer field and can be null if null a Trigger adds code.
– Paulo Henrique