BUG: Updating values in set causes same key value exception — oracle-tech

    Forum Stats

  • 3,716,134 Users
  • 2,242,961 Discussions
  • 7,845,841 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

BUG: Updating values in set causes same key value exception

4119029
4119029 Member Posts: 4
edited December 2019 in ODP.NET

The following code causes an exception, but ONLY when the updates are combined with inserts.  Using Oracle 18 xe.

            using (var context = serviceProvider.GetRequiredService<TableDbContext>())

            {

                for (int i = 0; i < 10; i++)

                {

                    context.Set<Table1>().Add(new Table1() { Value = i.ToString() });

                    context.Set<Table2>().Add(new Table2() { Value = i.ToString() });

                    context.Set<Table3>().Add(new Table3() { Value = i.ToString() });

                }

                await context.SaveChangesAsync();

                await context.Set<Table1>().ForEachAsync(o => o.Value = $"{o.Value}-{DateTime.Now.ToString()}");

                await context.Set<Table2>().ForEachAsync(o => o.Value = $"{o.Value}-{DateTime.Now.ToString()}");

                await context.Set<Table3>().ForEachAsync(o => o.Value = $"{o.Value}-{DateTime.Now.ToString()}");

                for (int i = 0; i < 10; i++)

                {

                    context.Set<Table1>().Add(new Table1() { Value = i.ToString() });

                    context.Set<Table2>().Add(new Table2() { Value = i.ToString() });

                    context.Set<Table3>().Add(new Table3() { Value = i.ToString() });

                }

                await context.SaveChangesAsync();

            }

    public class Table1

    {

        [Key]

        public long PersistenceId { get; set; }

        public string Value { get; set; }

    }

    public class TableDbContext : DbContext

    {

        public TableDbContext(DbContextOptions options)

            : base(options)

        { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Table1>()

                .ToTable("Table1", "EXC")

                .Property(x => x.PersistenceId).UseOracleIdentityColumn();

Tables 1, 2 and 3 have identical schemas and identical model builder code which was omitted for space.

InvalidOperationException: The instance of entity type 'Table1' cannot be tracked because another instance with the same key value for {'PersistenceId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

Dinand

Answers

Sign In or Register to comment.