Insert via EntityFramework "IDENTITY_INSERT is set to OFF" error

1

I have several models and for some reason one of them has the following error:

Cannot insert explicit value for identity column in table 'tbl_boleto' when IDENTITY_INSERT is set to OFF.

I remember that I have already entered a record, but now I can not 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();

The 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 self-contained. (int_ID) For all other models it is working.

Update : I discovered the reason for the error, but I do not 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 like this on other projects and I do not remember that I made a mistake. Am I making the wrong relationship?

    
asked by anonymous 03.03.2016 / 13:17

1 answer

1

The problem was how to create the relationship between tickets and ticketTXT

As it had been made boletoTXT had several tickets, so it made use of the foreign key.

In the model Tickets remained

public virtual ICollection<BoletoTXT> BoletoTXT { get; set; }

and in the TicketTXT model

public virtual boleto Boleto { get; set; }

and everything works out

    
03.03.2016 / 17:30