Topic
  • 4 replies
  • Latest Post - ‏2018-12-04T06:50:26Z by VishDev2018
AlexanderTanti
AlexanderTanti
3 Posts

Pinned topic Temporary Value error with NET Core 2 Entity Framework Core

‏2018-11-13T10:19:48Z | db2 entityframework ibm netcore2

Issue

An error is encountered when adding a new entity and updating or deleting another entity before calling SaveChanges().

 

Expected Behavior

New entities are added, updated entities are updated, deleted entities are deleted when SaveChanges is called.

Actual Behavior

Changes are persisted to the database (therefore the changes are applied, and the database is updated) however the new entities are not updated with their new Id's and an exception is thrown.

"The property 'Id' on entity type 'ParentTableModel' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property"

Steps to Reproduce the Problem

  1. Add one or multiple entities to a dbset
  2. Remove or update one or multiple entities of the same dbset
  3. Call SaveChanges()

Specifications

  • Version: .NET CORE 2.0, IBM.EntityFrameworkCore 1.2.2.100
  • Platform: Windows 10
  • Subsystem: Database Server IBM IDS_12_10_2000

Code-Snippets

 Model

    public class ParentTableModel {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Number { get; set; }
        public DateTime Timestamp { get; set; }
    }

 

 Model-Map

    public class ParentTableMap : IEntityTypeConfiguration<ParentTableModel> {

        public void Configure(EntityTypeBuilder<ParentTableModel> builder) {
            builder.Property(p => p.Id).HasColumnName("ptb_serial");
            builder.Property(p => p.Name).HasColumnName("ptb_name").HasMaxLength(10);
            builder.Property(p => p.Number).HasColumnName("ptb_number");
            builder.Property(p => p.Timestamp).HasColumnName("ptb_timestamp");

            builder.HasKey(p => p.Id);
            builder.ToTable("parenttable");
        }
    }

 

 Context

    public class TestDatabaseContext : DbContext {
        public DbSet<ParentTableModel> Parents { get; set; }

        public TestDatabaseContext(DbContextOptions<TestDatabaseContext> options) : base(options) {
            this.Parents = base.Set<ParentTableModel>();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);

            modelBuilder.HasDefaultSchema("informix");

            modelBuilder.ApplyConfiguration(new ParentTableMap());
        }

        public override int SaveChanges() {
            return base.SaveChanges();
        }
    }

 

 Repository

    public class ParentRepository: IParentRepository {

        private TestDatabaseContext _context;

 

        public ParentRepository(TestDatabaseContext databaseContext) {

            this._context = databaseContext;

        }

 

        public void AddParent(Parent parent) {

            ParentTableModel model = new ParentTableModel() {

                Name = parent.Name,

                Number = parent.Number,

                Timestamp = DateTime.Now

            };

 

            this._context.Parents.Add(model);

        }

 

        public void UpdateParent(Parent parent) {

            ParentTableModel model = this._context.Parents.SingleOrDefault(x => x.Number == parent.Number);

 

            model.Name = parent.Name;

 

            this._context.Parents.Update(model);

        }

 

        public Parent ReadParent(int number) {

            ParentTableModel model = this._context.Parents.SingleOrDefault(x => x.Number == number);

 

            return new Parent(model.Number, model.Name);

        }

 

        public void RemoveParent(int number) {

            ParentTableModel model = this._context.Parents.SingleOrDefault(x => x.Number == number);

 

            this._context.Parents.Remove(model);

        }

 

        public void SaveChanges() {

            this._context.SaveChanges();

        }

    }

 

 Program

       var firstParent = new Parent(1, "first");

       repository.AddParent(firstParent);

 

      repository.SaveChanges();

 

      var parent = new Parent(2, "second");

      repository.AddParent(parent);

 

 

      var thirdParent = new Parent(3, "third");

      repository.AddParent(thirdParent);

 

      var oldParent = repository.ReadParent(firstParent.Number);

      repository.RemoveParent(oldParent.Number);

 

      repository.SaveChanges(); //Exception thrown

 

Comments

  • Only occurs when adding and removing/updating before saving changes. 
    • Adding by itself works
    • Updating + Removing works
  • Data is persisted correctly, therefore the SQL generated is correct
  • The issue seems to originate when assigning the generated ID's of the new objects
Updated on 2018-11-13T10:26:54Z at 2018-11-13T10:26:54Z by AlexanderTanti
  • AlexanderTanti
    AlexanderTanti
    3 Posts

    Re: Temporary Value error with NET Core 2 Entity Framework Core

    ‏2018-12-03T06:47:36Z  

    Bump.

     

    Any help with this one ?

  • VishDev2018
    VishDev2018
    65 Posts

    Re: Temporary Value error with NET Core 2 Entity Framework Core

    ‏2018-12-04T04:59:51Z  

    Bump.

     

    Any help with this one ?

    Hi,

     this is currently an unsupported scenario. At present this scenario is supported in EntityFramework 6 for LUW databases. It is not yet supported in .NET Core EntityFramework provider.

     

    Thanks

    Vishwa

     

  • AlexanderTanti
    AlexanderTanti
    3 Posts

    Re: Temporary Value error with NET Core 2 Entity Framework Core

    ‏2018-12-04T06:09:32Z  

    Hi,

     this is currently an unsupported scenario. At present this scenario is supported in EntityFramework 6 for LUW databases. It is not yet supported in .NET Core EntityFramework provider.

     

    Thanks

    Vishwa

     

    Thanks for the reply.

    Adding and removing entities within the same transaction seems to be a common scenario.

    Is there a change planned to make this scenario work with either LUW database?

  • VishDev2018
    VishDev2018
    65 Posts

    Re: Temporary Value error with NET Core 2 Entity Framework Core

    ‏2018-12-04T06:50:26Z  

    Thanks for the reply.

    Adding and removing entities within the same transaction seems to be a common scenario.

    Is there a change planned to make this scenario work with either LUW database?

    Hi,

     yes we have plans of releasing support for this scenario in our .NET Core provider also. It will be for LUW servers only.

     

    Thanks

    Vishwa