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

Make InMemory provider enforce uniqueness of Alternate Keys #3850

Closed
philippecp opened this issue Nov 23, 2015 · 8 comments
Closed

Make InMemory provider enforce uniqueness of Alternate Keys #3850

philippecp opened this issue Nov 23, 2015 · 8 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.

Comments

@philippecp
Copy link

When adding two entities with the same alternate key in an InMemory database, no exception is thrown and both entities are added.

@rowanmiller rowanmiller changed the title InMemory provider doesn't enforce unique constraint of HasAlternateKey Make InMemory provider enforce uniqueness of Alternate Keys Dec 1, 2015
@rowanmiller rowanmiller added this to the Backlog milestone Dec 1, 2015
@rowanmiller rowanmiller added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Dec 1, 2015
@rowanmiller
Copy link
Contributor

Agreed that this would be a useful thing to add to the in-memory provider. We aren't planning to do this for initial RTM. Marking as up for grabs as this would make a good contribution too.

@divega divega added good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. good first issue This issue should be relatively straightforward to fix. labels May 31, 2019
@ajcvickers ajcvickers added good first issue This issue should be relatively straightforward to fix. and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. labels Aug 5, 2019
@android2221
Copy link

Is this issue still relevant? I'd like to try my hand at it.

@android2221
Copy link

android2221 commented May 2, 2021

With a cursory glance / test I was generating errors like this when adding an object with the same alternate key:
The instance of entity type 'Customer' cannot be tracked because another instance with the same key value for {'MyFakeAlternatekey'} is already being tracked. When attaching existing entities....

I got this when adding the second entity, before calling save.

This would be the ideal output right? If so, we might be able to close this one.

@AndriySvyryd
Copy link
Member

@android2221 Here's a test for this:

[ConditionalFact]
public void Throws_on_duplicate_alternate_key_values()
{
    var modelBuilder = InMemoryTestHelpers.Instance.CreateConventionBuilder();
    modelBuilder.Entity<MyEntityType>(
        eb =>
        {
            eb.HasAlternateKey("AlternateId");
        });

    var optionsBuilder = new DbContextOptionsBuilder()
        .UseModel(modelBuilder.FinalizeModel())
        .UseInMemoryDatabase("AlternateKeys");

    using (var context = new DbContext(optionsBuilder.Options))
    {
        context.Add(new MyEntityType { Id = 1, AlternateId = 11});
        context.SaveChanges();
    }

    using (var context = new DbContext(optionsBuilder.Options))
    {
        context.Add(new MyEntityType { Id = 2, AlternateId = 11 });
        Assert.Throws<DbUpdateException>(() => context.SaveChanges());
    }
}

private class MyEntityType
{
    public int Id { get; set; }
    public int AlternateId { get; set; }
}

@android2221
Copy link

@AndriySvyryd does this pass? In my testing I did not save the context between additions. If it passes, then we can close the issue right? If not, I'll dig deeper :)

@AndriySvyryd
Copy link
Member

@android2221 It doesn't pass

@android2221
Copy link

This one is outside my wheelhouse, I give up.

@ajcvickers
Copy link
Member

We recommend against using the in-memory provider for testing--see Testing EF Core Applications. While we have no plans to remove the in-memory provider, we will not be adding any new features to this provider because we believe valuable development time is better spent in other areas. When feasible, we plan to still fix regressions in existing behavior.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2022
@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed type-enhancement good first issue This issue should be relatively straightforward to fix. area-in-memory propose-close labels Oct 26, 2022
@ajcvickers ajcvickers removed this from the Backlog milestone Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.
Projects
None yet
Development

No branches or pull requests

7 participants