diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3721775a..30b991a6 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CA1416;CS8632 - 15.0.1 + 15.1.0 1.0.0 false false diff --git a/src/EfLocalDb.Tests/Tests.FindObject.verified.txt b/src/EfLocalDb.Tests/Tests.FindObject.verified.txt new file mode 100644 index 00000000..bab138b6 --- /dev/null +++ b/src/EfLocalDb.Tests/Tests.FindObject.verified.txt @@ -0,0 +1,4 @@ +{ + Id: 1, + Property: prop +} \ No newline at end of file diff --git a/src/EfLocalDb.Tests/Tests.FindObjectIgnoreFilters.verified.txt b/src/EfLocalDb.Tests/Tests.FindObjectIgnoreFilters.verified.txt new file mode 100644 index 00000000..72e9f134 --- /dev/null +++ b/src/EfLocalDb.Tests/Tests.FindObjectIgnoreFilters.verified.txt @@ -0,0 +1,4 @@ +{ + Id: 1, + Property: filtered +} \ No newline at end of file diff --git a/src/EfLocalDb.Tests/Tests.cs b/src/EfLocalDb.Tests/Tests.cs index 1929bff2..181dda29 100644 --- a/src/EfLocalDb.Tests/Tests.cs +++ b/src/EfLocalDb.Tests/Tests.cs @@ -359,6 +359,18 @@ public async Task Find() await Verify(database.Find(entity.Id)); } + [Fact] + public async Task FindObject() + { + var entity = new TestEntity + { + Property = "prop" + }; + await using var database = await instance.Build(); + await database.AddDataUntracked(entity); + await Verify(database.Context.Find(entity.Id)); + } + [Fact] public async Task FindMissing() { @@ -378,6 +390,18 @@ public async Task FindIgnoreFilters() await Verify(database.FindIgnoreFilters(entity.Id)); } + [Fact] + public async Task FindObjectIgnoreFilters() + { + var entity = new TestEntity + { + Property = "filtered" + }; + await using var database = await instance.Build(); + await database.AddDataUntracked(entity); + await Verify(database.Context.FindIgnoreFilters(entity.Id)); + } + [Fact] public async Task FindMissingIgnoreFilters() { diff --git a/src/EfLocalDb/SqlDatabase.cs b/src/EfLocalDb/SqlDatabase.cs index e12f7d93..59383e6c 100644 --- a/src/EfLocalDb/SqlDatabase.cs +++ b/src/EfLocalDb/SqlDatabase.cs @@ -110,7 +110,14 @@ public TDbContext NewDbContext(QueryTrackingBehavior? tracking = null) builder.ApplyQueryTracking(tracking); - return constructInstance(builder); + return Construct(builder); + } + + TDbContext Construct(DbContextOptionsBuilder builder) + { + var context = constructInstance(builder); + context.Model.SetRuntimeAnnotation("SqlDatabase", this); + return context; } public TDbContext NewConnectionOwnedDbContext(QueryTrackingBehavior? tracking = null) @@ -118,7 +125,7 @@ public TDbContext NewConnectionOwnedDbContext(QueryTrackingBehavior? tracking = var builder = DefaultOptionsBuilder.Build(); builder.UseSqlServer(Connection.ConnectionString, sqlOptionsBuilder); builder.ApplyQueryTracking(tracking); - return constructInstance(builder); + return Construct(builder); } public IReadOnlyList EntityTypes { get; private set; } = null!; diff --git a/src/EfLocalDb/SqlDatabase_Find.cs b/src/EfLocalDb/SqlDatabase_Find.cs index 9be196e7..8fd9a1ab 100644 --- a/src/EfLocalDb/SqlDatabase_Find.cs +++ b/src/EfLocalDb/SqlDatabase_Find.cs @@ -18,7 +18,7 @@ public Task FindIgnoreFilters(params object[] keys) where T : class => InnerFind(keys, true); - async Task InnerFind(object[] keys, bool ignoreFilters) where T : class + internal async Task InnerFind(object[] keys, bool ignoreFilters) where T : class { var (_, keyTypes, key, find) = entityKeyMap.Single(_ => _.Entity.ClrType == typeof(T)); @@ -50,7 +50,7 @@ public Task Find(params object[] keys) => public Task FindIgnoreFilters(params object[] keys) => InnerFind(true, keys); - async Task InnerFind(bool ignoreFilters, object[] keys) + internal async Task InnerFind(bool ignoreFilters, object[] keys) { var results = await FindResults(ignoreFilters, keys); diff --git a/src/EfLocalDb/SqlDatabase_FindContext.cs b/src/EfLocalDb/SqlDatabase_FindContext.cs new file mode 100644 index 00000000..99a69cc2 --- /dev/null +++ b/src/EfLocalDb/SqlDatabase_FindContext.cs @@ -0,0 +1,25 @@ +namespace EfLocalDb; + +public static class DbContextExtensions +{ + /// + /// Calls on all entity types and returns all resulting items. + /// + public static Task Find(this TDbContext context, params object[] keys) + where TDbContext : DbContext => + InnerFind(context, false, keys); + + /// + /// Calls on all entity types and returns all resulting items. + /// + public static Task FindIgnoreFilters(this TDbContext context, params object[] keys) + where TDbContext : DbContext => + InnerFind(context, true, keys); + + static Task InnerFind(this TDbContext context, bool ignoreFilters, object[] keys) + where TDbContext : DbContext + { + var database = (SqlDatabase) context.Model.FindRuntimeAnnotationValue("SqlDatabase")!; + return database.InnerFind(ignoreFilters, keys); + } +} \ No newline at end of file