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.
– Rabelos
@Rabelos Follow the suggestion of the error message and post the Inner Exception.
– ramaral
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"
– Rabelos
@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.
– Rabelos
Add to the question the contents of the "Inner Exception"
– Tony
The problem really was the field type Entity apparently does not accept the Numeric type in PK. Thank you all for your help.
– Rabelos