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

InMemoryDatabase auto increment with initial seed #15305

Closed
lcssanches opened this issue Apr 10, 2019 · 2 comments
Closed

InMemoryDatabase auto increment with initial seed #15305

lcssanches opened this issue Apr 10, 2019 · 2 comments

Comments

@lcssanches
Copy link

Auto increment/Identity initial value is not correct when the table has seeds

Steps to reproduce

Create a table with Identity Column Id

class CorConfig : IEntityTypeConfiguration<Cor>
{
        public void Configure(EntityTypeBuilder<Cor> builder)
        {
            builder.ToTable("Cores");
            builder.HasKey(r => r.Id).UseMySqlIdentityColumn();
            builder.Property(r => r.Nome).IsRequired().HasMaxLength(255);
            builder.Property(v => v.RGB).IsRequired().HasMaxLength(7);

            builder.HasData(GetSeed());
        }

        private ICollection<Cor> GetSeed()
        {
            return new List<Cor>()
            {
                new Cor(){Id=1, Nome="Black", RGB="#000"},
                new Cor(){Id=2, Nome="White", RGB="#fff"},
            };
        }
}

Create a test:
I comment query.Handler because just to simplify things, in fact it will execute DbSet.Add()

        [Test]
        public async Task CreateCorCommandHandlerTest()
        {
            var Context = CBERPContextFactory.Create();

            /*var query = new CreateCorCommandHandler(Context);
            var command = new CreateCorCommand();
            command.Nome = "123123";
            command.RGB = "#001122";
            var colecaoId = await query.Handle(command, CancellationToken.None);*/

            var cor = new Domain.Entities.Cor() { Nome = "Gray", RGB = "#CCC" };
            Context.Cores.Add(cor);
            await Context.SaveChangesAsync();
            var dbCor = Context.Cores.Where(p => p.Id == cor.Id).SingleOrDefault();
            CBERPContextFactory.Destroy(Context);
            var colecaoId = cor.Id;

            //Assert
            Assert.IsInstanceOf<long>(colecaoId);
            Assert.NotZero(colecaoId);                       
            Assert.NotNull(dbCor);
            Assert.AreEqual(cor.Nome, dbCor.Nome);
            Assert.AreEqual(cor.RGB, dbCor.RGB);
        }

I expect this test to pass, but instead, I receive the following exception
Message: System.ArgumentException : An item with the same key has already been added. Key: 1

If I change the table to be empty by default, and change the test to add more than one entity on database, the auto increment work as expected, resulting in IDs 1 and 2.

Further technical details

EF Core version: 2.2.4
Database Provider: Microsoft.EntityFrameworkCore.InMemory 2.2.4
Operating system: Windows 10
IDE: (e.g. Visual Studio 2017 15.4) VS2019

@ajcvickers
Copy link
Member

Duplicate of #6872

@emilborowiec
Copy link

This bug is still occurring in preview 5 of EF Core 3.0.

I actually don't think it is a duplicate of #6872 which seems to be more about resetting value generator between the tests. Here the problem is that value generator does not take into account explicit IDs passed in HasData calls and tries to start from 1 for integers.

Please consider reopening.

@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

3 participants