Insert via Entityframework error "IDENTITY_INSERT is set to OFF"

Asked

Viewed 966 times

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.

  • You can try adding Annotation [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] in PK

  • Probably your table was changed after you generated your template..

  • If the database is sql server try exec SET IDENTITY_INSERT to ON. SET IDENTITY_INSERT tbl_boleto ON;&#xA;GO

  • Remove Clienteid before giving the add and try again.

  • Philip the Clienteid I need to send, what could not send would be the Boletoid. @Marconcilio Souza, worse than not hear change.

  • If you play the Boletoid it record?

  • Your database is sql server:?

  • 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

  • Why do you rename your columns? Already tried only [Key]&#xA; public int int_ID{ get; set;

  • I updated the question @Marconcilio Souza

  • [Foreignkey("int_ID")] ...

Show 7 more comments

1 answer

1


the problem was how to create relationships between boletos and boletoTXT

as had been made boletoTXT had several boletos, so he made use of the foreign key.

In the model Boletos got

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

and the Boletostxt model

public virtual boleto Boleto { get; set; }

and it’ll all work out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.