efcore icon indicating copy to clipboard operation
efcore copied to clipboard

.NET Framework 4.7.2 InMemory EnsureCreated with seeded data then Add new entity throws ArgumentException : An item with the same key has already been added.

Open gojanpaolo opened this issue 8 years ago • 4 comments

I get the following exception on .NET framework 4.7.2 but not on .NET Core 2.1.

Exception message: System.ArgumentException: 'An item with the same key has already been added.'
Stack trace:    
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.Create(IUpdateEntry entry)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryStore.ExecuteTransaction(IReadOnlyList`1 entries, IDiagnosticsLogger`1 updateLogger)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryDatabase.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at ClassLibrary1.Tests.EnsureCreated_then_Add_Blog() in

Steps to reproduce

using Microsoft.EntityFrameworkCore;
using System;
using Xunit;

public class Tests
{
    private readonly DbContextOptions<Context> _options;

    public Tests()
    {
        _options = new DbContextOptionsBuilder<Context>()
            .UseInMemoryDatabase(Guid.NewGuid().ToString())
            .Options;
    }

    [Fact]
    public void Add_Blog()
    {
        using (var ctx = new Context(_options))
        {
            ctx.Add(new Blog());
            ctx.SaveChanges();
        }
    }

    [Fact]
    public void EnsureCreated_then_Add_Blog()
    {
        using (var ctx = new Context(_options))
        {
            ctx.Database.EnsureCreated();
        }

        using (var ctx = new Context(_options))
        {
            ctx.Add(new Blog());
            ctx.SaveChanges();
        }
    }
}
public class Context : DbContext
{
    public Context(DbContextOptions<Context> options) : base(options) { }
    public DbSet<Blog> Blog { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>(builder =>
        {
            builder.HasData(
                new Blog { BlogId = 1 });
        });
    }
}
public class Blog
{
    public int BlogId { get; set; }
}

Further technical details

EF Core version: 2.1.3 Database Provider: Microsoft.EntityFrameworkCore.InMemory Operating system: Win7 IDE: Visual Studio 2017 15.4

gojanpaolo avatar Sep 13 '18 18:09 gojanpaolo

Issue seems to be fixed on 2.2.0-preview2-35157.

gojanpaolo avatar Sep 13 '18 18:09 gojanpaolo

Issue seems to be fixed on 2.2.0-preview2-35157.

With this version I also get the error

Alexxtheonly avatar Sep 14 '18 09:09 Alexxtheonly

So I just tested again on a new solution and issue still exists in 2.2.0-preview2-35157

gojanpaolo avatar Sep 14 '18 14:09 gojanpaolo

Duplicate of #6872, since in-memory value generation from keys does not currently consider any existing values.

ajcvickers avatar Sep 14 '18 19:09 ajcvickers