1
I have several models and for some reason one of them presents the following error:
Cannot insert explicit value for identity column in table 'tbl_boleto' when IDENTITY_INSERT is set to OFF.
I remember I once entered a record, but now I can’t enter it anymore.
var NewBoleto = new Boleto
{
ClienteId = 3,
DataVencimento = DateTime.Today,
Valor = 4,
DataBaixa=null,
DataEmissao = DateTime.Now,
DataReferencia= DateTime.Today,
DataPagamento = null
};
db.Boletos.Add(NewBoleto);
db.SaveChanges();
A model:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WMB.Models
{
[Table("tbl_boleto")]
public class Boleto
{
[Key]
[Column("int_ID")]
public int BoletoId { get; set; }
[Column("int_IDC")]
public int ClienteId { get; set; }
[Column("sdt_Dataemissao")]
public DateTime DataEmissao { get; set; }
[Column("sdt_DataVencimento")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[Required(ErrorMessage = "Data de Vencimento é requerida")]
[Display(Name = "Data de Vencimento")]
[DataType(DataType.Date)]
public DateTime DataVencimento { get; set; }
[Column("sdt_dataPagamento")]
public DateTime? DataPagamento { get; set; }
[Column("sdt_dataBaixa")]
public DateTime? DataBaixa { get; set; }
[Column("sdt_DataReferencia")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[Display(Name = "Referência 01/mm/aaaa")]
[DataType(DataType.Date)]
public DateTime DataReferencia { get; set; }
[Column("cur_valor")]
public decimal? Valor { get; set; }
[Column("cur_valorPago")]
public decimal? ValorPago { get; set; }
[Column("int_status")]
public byte Status { get; set; }
[Column("int_TotalMeses")]
public byte? TotalMeses { get; set; }
[Column("int_Tipo")]
public byte Tipo { get; set; }
[Column("int_ViewClient")]
public byte? QtdVisualizacoes { get; set; }
public virtual Cliente Cliente { get; set; }
//[ForeignKey("BoletoId")]
//public virtual BoletoTXT boletoTxt { get; set; }
}
}
When looking through the SQL profile for some unknown reason for this model it is sending the PK field which is autonumeric. (int_ID) For all other models is working.
Updating: I found the reason for the mistake, but I don’t know why. I have another Model called Boletotxt that has a connection with Boleto N-1
[Table("tbl_boleto_TXT")]
public class BoletoTXT
{
[Key]
[Column("int_ID")]
public int BoletoTXTId { get; set; }
[Column("int_IDBoleto")]
public int BoletoId { get; set; }
[Column("str_Mensagem")]
public string Menssagem { get; set; }
[ForeignKey("BoletoId")]
public virtual ICollection<boleto> boleto { get; set; }
}
The problem is this line
[ForeignKey("BoletoId")]
public virtual ICollection<boleto> boleto { get; set; }
But I’ve used it in other projects and I don’t remember making a mistake. I’m making the wrong relationship?
Did you generate your model through Database First? If you are using Fluent API post as is the configuration.
– Randrade
You can try adding Annotation
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
in PK– Randrade
Probably your table was changed after you generated your template..
– Marco Souza
If the database is sql server try exec SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT tbl_boleto ON;
GO
– Marco Souza
Remove Clienteid before giving the add and try again.
– Filipe Oliveira
Philip the Clienteid I need to send, what could not send would be the Boletoid. @Marconcilio Souza, worse than not hear change.
– Dorathoto
If you play the Boletoid it record?
– Marco Souza
Your database is sql server:?
– Marco Souza
MS SQL server, in theory to make an Insert should not send Boletoid which is the key (pk) but even tried and does not write.. same error
– Dorathoto
Why do you rename your columns? Already tried only
[Key]
 public int int_ID{ get; set;
– Marco Souza
I updated the question @Marconcilio Souza
– Dorathoto
[Foreignkey("int_ID")] ...
– Marco Souza