Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inmemory DB throws ArgumentException when adding an item in a second service collection #16689

Closed
JanEggers opened this issue Jul 22, 2019 · 4 comments

Comments

@JanEggers
Copy link

JanEggers commented Jul 22, 2019

im using efcore in memory 2.2.3

and this is throwing argument exception when adding s3 (savechanges):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace EntityFrameworkTestBugRepro
{
    public class EntityFrameworkTestBugRepro
    {

        public class TestContext : DbContext
        {
            public TestContext(DbContextOptions<TestContext> options)
                : base(options)
            {

            }

            public DbSet<TestObject> Items { get; set; }
        }

        public class TestObject
        {
            [Key]
            public int Id { get; set; }

            public string Name { get; set; }
        }

        [Fact]
        public async Task InMemoryStoreWorks_Repro()
        {
            await Task.Yield();

            var root = new InMemoryDatabaseRoot();

            using (var services = new ServiceCollection()
                .AddDbContext<TestContext>(o => o.UseInMemoryDatabase("Foo", root))
                .BuildServiceProvider())
            {
                using (var scope = services.CreateScope())
                {
                    var dbcontext = scope.ServiceProvider.GetRequiredService<TestContext>();

                    dbcontext.Items.Add(new TestObject { Name = "s1" });

                    dbcontext.SaveChanges();
                }


                using (var scope = services.CreateScope())
                {
                    var dbcontext = scope.ServiceProvider.GetRequiredService<TestContext>();

                    dbcontext.Items.Add(new TestObject { Name = "s2" });

                    dbcontext.SaveChanges();
                }

            }

            using (var services = new ServiceCollection()
                .AddDbContext<TestContext>(o => o.UseInMemoryDatabase("Foo", root))
                .BuildServiceProvider())
            {
                using (var scope = services.CreateScope())
                {
                    var dbcontext = scope.ServiceProvider.GetRequiredService<TestContext>();

                    Assert.Equal(2, dbcontext.Items.Count());

                    dbcontext.Items.Add(new TestObject { Name = "s3" });

                    dbcontext.SaveChanges();
                }
            }
        }
    }
}
ArgumentException: Ein Element mit dem gleichen Schlüssel wurde bereits hinzugefügt.
   bei System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   bei System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   bei Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.Create(IUpdateEntry entry)
   bei Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryStore.ExecuteTransaction(IReadOnlyList`1 entries, IDiagnosticsLogger`1 updateLogger)
   bei Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryDatabase.SaveChanges(IReadOnlyList`1 entries)
   bei Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   bei Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   bei Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   bei Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
@ajcvickers
Copy link
Member

Duplicate of #6872

@ajcvickers ajcvickers marked this as a duplicate of #6872 Jul 22, 2019
@JanEggers
Copy link
Author

@ajcvickers why is this a duplicate? The other issue is about resetting the key value generator and for my Szenario it has to consider existing rows. Can you please Post a workaound because it is not clear for me from the referenced issue

@JanEggers
Copy link
Author

never mind i updated to 3.0.0 preview 6 and with that version it is behaving correctly

@ajcvickers
Copy link
Member

@JanEggers The fix for that issue also included taking into account existing IDs in the table. It was basically a complete overhaul of the strategy to make it behave more like the relational equivalents.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants