Error updating entries in Entity Framework

Asked

Viewed 5,990 times

5

I have the following code for mapping my application, but when I will try to make a insert gives the following error.

An error occurred while updating the Entries. See the Inner Exception for Details

I’ve made that mistake before I only traded one HasOptional by a HasRequired and it worked but this time I’m not finding a solution to this mistake.

Class:

public partial class REGISTRO_PERGUNTAS
{
    public REGISTRO_PERGUNTAS()
    {
        this.REGISTRO_RESPOSTAS = new List<REGISTRO_RESPOSTAS>();
    }

    public decimal ID_PERGUNTA { get; set; }
    public int ID_ARQUIVO { get; set; }
    public byte ID_TIPO { get; set; }
    public int? NUMERO_PERGUNTA { get; set; }
    public string PERGUNTA { get; set; }
    public string USUARIO_PERGUNTA { get; set; }
    public Nullable<int> ID_AREA { get; set; }
    public Nullable<System.DateTime> DT_PERGUNTA { get; set; }
    public Nullable<int> SITUACAO { get; set; }
    public virtual AREAS_RESPONSAVEIS AREAS_RESPONSAVEIS { get; set; }
    public virtual ICollection<REGISTRO_RESPOSTAS> REGISTRO_RESPOSTAS { get; set; }
    public virtual REGISTRO_QUESTIONARIO REGISTRO_QUESTIONARIO { get; set; }
    public virtual TIPOS_PERGUNTA TIPOS_PERGUNTA { get; set; }
}

Mapping:

public class REGISTRO_PERGUNTASMap : EntityTypeConfiguration<REGISTRO_PERGUNTAS>
{
    public REGISTRO_PERGUNTASMap() {
        // Primary Key
        this.HasKey(t => t.ID_PERGUNTA);

        // Properties
        this.Property(t => t.PERGUNTA).IsRequired().HasMaxLength(300);
        this.Property(t => t.USUARIO_PERGUNTA).HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("REGISTRO_PERGUNTAS");
        this.Property(t => t.ID_PERGUNTA).HasColumnName("ID_PERGUNTA");
        this.Property(t => t.ID_ARQUIVO).HasColumnName("ID_ARQUIVO");
        this.Property(t => t.ID_TIPO).HasColumnName("ID_TIPO");
        this.Property(t => t.NUMERO_PERGUNTA).HasColumnName("NUMERO_PERGUNTA");
        this.Property(t => t.PERGUNTA).HasColumnName("PERGUNTA");
        this.Property(t => t.USUARIO_PERGUNTA).HasColumnName("USUARIO_PERGUNTA");
        this.Property(t => t.ID_AREA).HasColumnName("ID_AREA");
        this.Property(t => t.DT_PERGUNTA).HasColumnName("DT_PERGUNTA");
        this.Property(t => t.SITUACAO).HasColumnName("SITUACAO");

        // Relationships
        this.HasRequired(t => t.AREAS_RESPONSAVEIS)
            .WithMany(t => t.REGISTRO_PERGUNTAS)
            .HasForeignKey(d => d.ID_AREA);
        this.HasRequired(t => t.REGISTRO_QUESTIONARIO)
            .WithMany(t => t.REGISTRO_PERGUNTAS)
            .HasForeignKey(d => d.ID_ARQUIVO);
        this.HasRequired(t => t.TIPOS_PERGUNTA)
            .WithMany(t => t.REGISTRO_PERGUNTAS)
            .HasForeignKey(d => d.ID_TIPO);
    }
}
  • I am. Using ASP.NET MVC.

  • 1

    @Rabelos Follow the suggestion of the error message and post the Inner Exception.

  • The error that gives is this "Cannot insert an explicit value for the identity column in table 'REGISTRO_PERGUNTAS' when IDENTITY_INSERT is set to OFF"

  • @Tiago Silva this is my problem I am not inserting the PK but the error continues. Searching the net I saw other people with the same problem but nobody reported any correction.

  • Add to the question the contents of the "Inner Exception"

  • 1

    The problem really was the field type Entity apparently does not accept the Numeric type in PK. Thank you all for your help.

Show 1 more comment

1 answer

5

Include in your context a override for the event SaveChanges as follows:

    public override int SaveChanges()
    {          
        try
        {
            return base.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors) // <-- Coloque um Breakpoint aqui para conferir os erros de validação.
            {
                Console.WriteLine("Entidade do tipo \"{0}\" no estado \"{1}\" tem os seguintes erros de validação:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Erro: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }
        catch (DbUpdateException e)
        {
            foreach (var eve in e.Entries)
            {
                Console.WriteLine("Entidade do tipo \"{0}\" no estado \"{1}\" tem os seguintes erros de validação:",
                    eve.Entity.GetType().Name, eve.State);
            }
            throw;
        }
        catch (SqlException s) {
            Console.WriteLine("- Message: \"{0}\", Data: \"{1}\"",
                        s.Message, s.Data);
            throw;
        }
    }

You can put a breakpoint as suggested or check the messages sent to the Console.

Browser other questions tagged

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