Skip to content

Commit 06346ab

Browse files
authored
add Find for Object (#477)
1 parent df9506a commit 06346ab

7 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CA1416;CS8632</NoWarn>
5-
<Version>15.0.1</Version>
5+
<Version>15.1.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<ContinuousIntegrationBuild>false</ContinuousIntegrationBuild>
88
<CheckEolTargetFramework>false</CheckEolTargetFramework>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
Id: 1,
3+
Property: prop
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
Id: 1,
3+
Property: filtered
4+
}

src/EfLocalDb.Tests/Tests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,18 @@ public async Task Find()
359359
await Verify(database.Find(entity.Id));
360360
}
361361

362+
[Fact]
363+
public async Task FindObject()
364+
{
365+
var entity = new TestEntity
366+
{
367+
Property = "prop"
368+
};
369+
await using var database = await instance.Build();
370+
await database.AddDataUntracked(entity);
371+
await Verify(database.Context.Find(entity.Id));
372+
}
373+
362374
[Fact]
363375
public async Task FindMissing()
364376
{
@@ -378,6 +390,18 @@ public async Task FindIgnoreFilters()
378390
await Verify(database.FindIgnoreFilters(entity.Id));
379391
}
380392

393+
[Fact]
394+
public async Task FindObjectIgnoreFilters()
395+
{
396+
var entity = new TestEntity
397+
{
398+
Property = "filtered"
399+
};
400+
await using var database = await instance.Build();
401+
await database.AddDataUntracked(entity);
402+
await Verify(database.Context.FindIgnoreFilters(entity.Id));
403+
}
404+
381405
[Fact]
382406
public async Task FindMissingIgnoreFilters()
383407
{

src/EfLocalDb/SqlDatabase.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,22 @@ public TDbContext NewDbContext(QueryTrackingBehavior? tracking = null)
110110

111111
builder.ApplyQueryTracking(tracking);
112112

113-
return constructInstance(builder);
113+
return Construct(builder);
114+
}
115+
116+
TDbContext Construct(DbContextOptionsBuilder<TDbContext> builder)
117+
{
118+
var context = constructInstance(builder);
119+
context.Model.SetRuntimeAnnotation("SqlDatabase", this);
120+
return context;
114121
}
115122

116123
public TDbContext NewConnectionOwnedDbContext(QueryTrackingBehavior? tracking = null)
117124
{
118125
var builder = DefaultOptionsBuilder.Build<TDbContext>();
119126
builder.UseSqlServer(Connection.ConnectionString, sqlOptionsBuilder);
120127
builder.ApplyQueryTracking(tracking);
121-
return constructInstance(builder);
128+
return Construct(builder);
122129
}
123130

124131
public IReadOnlyList<IEntityType> EntityTypes { get; private set; } = null!;

src/EfLocalDb/SqlDatabase_Find.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Task<T> FindIgnoreFilters<T>(params object[] keys)
1818
where T : class =>
1919
InnerFind<T>(keys, true);
2020

21-
async Task<T> InnerFind<T>(object[] keys, bool ignoreFilters) where T : class
21+
internal async Task<T> InnerFind<T>(object[] keys, bool ignoreFilters) where T : class
2222
{
2323
var (_, keyTypes, key, find) = entityKeyMap.Single(_ => _.Entity.ClrType == typeof(T));
2424

@@ -50,7 +50,7 @@ public Task<object> Find(params object[] keys) =>
5050
public Task<object> FindIgnoreFilters(params object[] keys) =>
5151
InnerFind(true, keys);
5252

53-
async Task<object> InnerFind(bool ignoreFilters, object[] keys)
53+
internal async Task<object> InnerFind(bool ignoreFilters, object[] keys)
5454
{
5555
var results = await FindResults(ignoreFilters, keys);
5656

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace EfLocalDb;
2+
3+
public static class DbContextExtensions
4+
{
5+
/// <summary>
6+
/// Calls <see cref="DbContext.FindAsync(Type,object[])" /> on all entity types and returns all resulting items.
7+
/// </summary>
8+
public static Task<object> Find<TDbContext>(this TDbContext context, params object[] keys)
9+
where TDbContext : DbContext =>
10+
InnerFind(context, false, keys);
11+
12+
/// <summary>
13+
/// Calls <see cref="DbContext.FindAsync(Type,object[])" /> on all entity types and returns all resulting items.
14+
/// </summary>
15+
public static Task<object> FindIgnoreFilters<TDbContext>(this TDbContext context, params object[] keys)
16+
where TDbContext : DbContext =>
17+
InnerFind(context, true, keys);
18+
19+
static Task<object> InnerFind<TDbContext>(this TDbContext context, bool ignoreFilters, object[] keys)
20+
where TDbContext : DbContext
21+
{
22+
var database = (SqlDatabase<TDbContext>) context.Model.FindRuntimeAnnotationValue("SqlDatabase")!;
23+
return database.InnerFind(ignoreFilters, keys);
24+
}
25+
}

0 commit comments

Comments
 (0)