0

I'm pulling in a bunch of data from a public api, and I'm using EF code first to dump it into an internal Oracle DB. I'm using a trigger in the database to auto-increment the primary key value, and I believe this is where the issue is stemming from. Here is the error message:

System.InvalidOperationException: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'SocrataNightlyDump.Models.INCIDENT_CRIME_READBACK' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

System.InvalidOperationException: Saving or accepting changes failed because more than one entity of type 'SocrataNightlyDump.Models.INCIDENT_CRIME_READBACK' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

at System.Data.Entity.Core.Objects.ObjectStateManager.FixupKey(EntityEntry entry)
at System.Data.Entity.Core.Objects.EntityEntry.AcceptChanges()
at System.Data.Entity.Core.Objects.ObjectContext.AcceptAllChanges()
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) --- End of inner exception stack trace --- at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.b__27() at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at SocrataNightlyDump.Program.Main(String[] args) in C:\Users\Austin\Documents\SocrataNightlyDump\SocrataNightlyDump\Program.cs:line 71

I placed the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute on the primary key value in the generated model class, but this did not solve the issue. The entries are still saved in the database, but it's still throwing this exception when I run the program. Has anyone run into this issue when using an Oracle database and having a trigger for auto-incrementation?

EDIT: Here is the relevant code in my model class:

[Table("INCIDENT_CRIME_SOCRATA_USER.INCIDENT_CRIME_READBACK")]
    public partial class INCIDENT_CRIME_READBACK
    {
        [Column("ID")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public decimal primaryKeyId { get; set; }
        ...
    }
Otto45
  • 555
  • 2
  • 6
  • 20

1 Answers1

0

Your model has a correct configuration, but you´ll have to use a sequence. EF6 is quite limited in the way it handles database generated fields, specially PKs.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • I have set up a sequence and trigger on insert in my database, the issue is that despite having the `[DatabaseGenerated(DatabaseGeneratedOption.Identity)]` attribute on my PK variable in the model class, it still throws the above error. – Otto45 Apr 26 '16 at 18:06