Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CA1416;CS8632</NoWarn>
<Version>15.0.1</Version>
<Version>15.1.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<ContinuousIntegrationBuild>false</ContinuousIntegrationBuild>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
Expand Down
4 changes: 4 additions & 0 deletions src/EfLocalDb.Tests/Tests.FindObject.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Id: 1,
Property: prop
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Id: 1,
Property: filtered
}
24 changes: 24 additions & 0 deletions src/EfLocalDb.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down
11 changes: 9 additions & 2 deletions src/EfLocalDb/SqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,22 @@ public TDbContext NewDbContext(QueryTrackingBehavior? tracking = null)

builder.ApplyQueryTracking(tracking);

return constructInstance(builder);
return Construct(builder);
}

TDbContext Construct(DbContextOptionsBuilder<TDbContext> builder)
{
var context = constructInstance(builder);
context.Model.SetRuntimeAnnotation("SqlDatabase", this);
return context;
}

public TDbContext NewConnectionOwnedDbContext(QueryTrackingBehavior? tracking = null)
{
var builder = DefaultOptionsBuilder.Build<TDbContext>();
builder.UseSqlServer(Connection.ConnectionString, sqlOptionsBuilder);
builder.ApplyQueryTracking(tracking);
return constructInstance(builder);
return Construct(builder);
}

public IReadOnlyList<IEntityType> EntityTypes { get; private set; } = null!;
Expand Down
4 changes: 2 additions & 2 deletions src/EfLocalDb/SqlDatabase_Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Task<T> FindIgnoreFilters<T>(params object[] keys)
where T : class =>
InnerFind<T>(keys, true);

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

Expand Down Expand Up @@ -50,7 +50,7 @@ public Task<object> Find(params object[] keys) =>
public Task<object> FindIgnoreFilters(params object[] keys) =>
InnerFind(true, keys);

async Task<object> InnerFind(bool ignoreFilters, object[] keys)
internal async Task<object> InnerFind(bool ignoreFilters, object[] keys)
{
var results = await FindResults(ignoreFilters, keys);

Expand Down
25 changes: 25 additions & 0 deletions src/EfLocalDb/SqlDatabase_FindContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace EfLocalDb;

public static class DbContextExtensions
{
/// <summary>
/// Calls <see cref="DbContext.FindAsync(Type,object[])" /> on all entity types and returns all resulting items.
/// </summary>
public static Task<object> Find<TDbContext>(this TDbContext context, params object[] keys)
where TDbContext : DbContext =>
InnerFind(context, false, keys);

/// <summary>
/// Calls <see cref="DbContext.FindAsync(Type,object[])" /> on all entity types and returns all resulting items.
/// </summary>
public static Task<object> FindIgnoreFilters<TDbContext>(this TDbContext context, params object[] keys)
where TDbContext : DbContext =>
InnerFind(context, true, keys);

static Task<object> InnerFind<TDbContext>(this TDbContext context, bool ignoreFilters, object[] keys)
where TDbContext : DbContext
{
var database = (SqlDatabase<TDbContext>) context.Model.FindRuntimeAnnotationValue("SqlDatabase")!;
return database.InnerFind(ignoreFilters, keys);
}
}