-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. |
Is this issue still relevant? I'd like to try my hand at it. |
With a cursory glance / test I was generating errors like this when adding an object with the same alternate key: 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. |
@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; }
} |
@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 :) |
@android2221 It doesn't pass |
This one is outside my wheelhouse, I give up. |
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. |
When adding two entities with the same alternate key in an InMemory database, no exception is thrown and both entities are added.
The text was updated successfully, but these errors were encountered: