Thanksgiving project: lookup entities by primary key, alternate key, or foreign key#29686
Merged
ajcvickers merged 3 commits intomainfrom Dec 3, 2022
Merged
Thanksgiving project: lookup entities by primary key, alternate key, or foreign key#29686ajcvickers merged 3 commits intomainfrom
ajcvickers merged 3 commits intomainfrom
Conversation
Contributor
Author
|
Benchmarks:
using System.ComponentModel.DataAnnotations.Schema;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
BenchmarkRunner.Run<Benchmarks>();
public class Benchmarks
{
private static readonly TestContext Context;
private static readonly IProperty AlternateKeyProperty;
private static readonly IProperty ForeignKeyProperty;
private static readonly IProperty NonKeyProperty;
static Benchmarks()
{
using (var context = new TestContext())
{
context.Seed();
}
Context = new TestContext();
Context.Principals.Include(e => e.Dependent1s).Include(e => e.Dependent2s).Load();
Context.ChangeTracker.AutoDetectChangesEnabled = false;
AlternateKeyProperty = Context.Principals.EntityType.FindProperty(nameof(Principal.AltId))!;
ForeignKeyProperty = Context.Dependent2s.EntityType.FindProperty(nameof(Dependent2.PrincipalId))!;
NonKeyProperty = Context.Dependent2s.EntityType.FindProperty(nameof(Dependent2.NonKey))!;
}
[Benchmark]
public void DbSet_Find_by_primary_key()
{
var entity = Context.Principals.Find(501);
}
[Benchmark]
public void Search_entries_by_primary_key()
{
foreach (var entry in Context.ChangeTracker.Entries<Principal>())
{
if (entry.Entity.Id == 501)
{
return;
}
}
}
[Benchmark]
public void DbSet_Local_FindEntryByKey()
{
var entry = Context.Principals.Local.FindEntryByKey(501);
}
[Benchmark]
public void Search_entries_by_alternate_key()
{
foreach (var entry in Context.ChangeTracker.Entries<Principal>())
{
if (entry.Entity.AltId == 501)
{
return;
}
}
}
[Benchmark]
public void DbSet_Local_FindEntryByProperty_alternate_key()
{
var entry = Context.Principals.Local.FindEntryByProperty(AlternateKeyProperty, 501);
}
[Benchmark]
public void Search_entries_by_foreign_key()
{
var results = new List<Dependent2>();
foreach (var entry in Context.ChangeTracker.Entries<Dependent2>())
{
if (entry.Entity.PrincipalId == 501)
{
results.Add(entry.Entity);
}
}
}
[Benchmark]
public void DbSet_Local_GetEntriesByProperty_foreign_key()
{
var results = new List<Dependent2>();
foreach (var entry in Context.Dependent2s.Local.GetEntriesByProperty(ForeignKeyProperty, 501))
{
if (entry.Entity.PrincipalId == 501)
{
results.Add(entry.Entity);
}
}
}
[Benchmark]
public void Search_entries_by_non_key()
{
var results = new List<Dependent2>();
foreach (var entry in Context.ChangeTracker.Entries<Dependent2>())
{
if (entry.Entity.NonKey == 501)
{
results.Add(entry.Entity);
}
}
}
[Benchmark]
public void DbSet_Local_GetEntriesByProperty_non_key()
{
var results = new List<Dependent2>();
foreach (var entry in Context.Dependent2s.Local.GetEntriesByProperty(NonKeyProperty, 501))
{
if (entry.Entity.NonKey == 501)
{
results.Add(entry.Entity);
}
}
}
}
public class TestContext : DbContext
{
public DbSet<Principal> Principals { get; set; } = null!;
public DbSet<Dependent1> Dependent1s { get; set; } = null!;
public DbSet<Dependent2> Dependent2s { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseInMemoryDatabase("X");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Principal>()
.HasMany(e => e.Dependent2s)
.WithOne(e => e.Principal)
.HasPrincipalKey(e => e.AltId);
}
public void Seed()
{
for (var i = 1; i <= 1000; i++)
{
var principal = new Principal { Id = i, AltId = i };
for (var j = 1; j <= 20; j++)
{
principal.Dependent1s.Add(new Dependent1 { Id = (i * 20) + j, NonKey = i });
principal.Dependent2s.Add(new Dependent2 { Id = (i * 20) + j, NonKey = i });
}
Add(principal);
}
SaveChanges();
}
}
public class Principal
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AltId { get; set; }
public List<Dependent1> Dependent1s { get; } = new();
public List<Dependent2> Dependent2s { get; } = new();
}
public class Dependent1
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int? PrincipalId { get; set; }
public int NonKey { get; set; }
public Principal? Principal { get; set; }
}
public class Dependent2
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int? PrincipalId { get; set; }
public int NonKey { get; set; }
public Principal? Principal { get; set; }
} |
14e8736 to
58ff853
Compare
AndriySvyryd
reviewed
Dec 2, 2022
src/EFCore/ChangeTracking/Internal/CompositePrincipalKeyValueFactory.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
src/EFCore/ChangeTracking/Internal/SimplePrincipalKeyValueFactory.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
Comment on lines
+605
to
+608
| /// <para> | ||
| /// Call <see cref="ChangeTracker.DetectChanges" /> before calling this method to ensure all entries returned reflect | ||
| /// up-to-date state. | ||
| /// </para> |
Member
There was a problem hiding this comment.
We might need something in the API to indicate whether a DetectChanges is needed for any particular call. Or perhaps we just call it always ourselves unless the user opts-out temporarily.
Contributor
Author
There was a problem hiding this comment.
Will do this in another PR so that it can be reviewed.
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
reviewed
Dec 2, 2022
AndriySvyryd
approved these changes
Dec 2, 2022
58ff853 to
39c59fc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #29685.