Skip to Main Content

ODP.NET

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

BUG: Updating values in set causes same key value exception

4119029Oct 24 2019 — edited Dec 7 2019

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.

Comments

Processing

Post Details

Added on Oct 24 2019
2 comments
276 views