Skip to content

Commit

Permalink
feat: support ef7 ExecuteDeleteAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jan 16, 2023
1 parent 81bdfe4 commit f1306fd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
37 changes: 26 additions & 11 deletions src/Sitko.Core.Repository.EntityFrameworkCore/EFRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ namespace Sitko.Core.Repository.EntityFrameworkCore;
public interface IEFRepository : IRepository
{
Task<int> DeleteAllRawAsync(string conditions, CancellationToken cancellationToken = default);
Task<int> DeleteAllAsync(CancellationToken cancellationToken = default);
}

public interface IEFRepository<TEntity> : IEFRepository where TEntity : class, IEntity
{
Task<int> DeleteAllAsync(Expression<Func<TEntity, bool>> where, CancellationToken cancellationToken = default);
}

public abstract class EFRepository<TEntity, TEntityPk, TDbContext> :
BaseRepository<TEntity, TEntityPk, EFRepositoryQuery<TEntity>>, IEFRepository
BaseRepository<TEntity, TEntityPk, EFRepositoryQuery<TEntity>>, IEFRepository<TEntity>
where TEntity : class, IEntity<TEntityPk> where TDbContext : DbContext where TEntityPk : notnull
{
private readonly TDbContext dbContext;
Expand All @@ -30,6 +36,25 @@ public abstract class EFRepository<TEntity, TEntityPk, TDbContext> :
repositoryLock = repositoryContext.RepositoryLock;
}

public Task<int> DeleteAllRawAsync(string conditions, CancellationToken cancellationToken = default)
{
var tableName = dbContext.Model.FindEntityType(typeof(TEntity))?.GetSchemaQualifiedTableName() ??
throw new InvalidOperationException($"Can't find table name for entity {typeof(TEntity)}");
return ExecuteDbContextOperationAsync(context => context.Database.ExecuteSqlRawAsync(
$"DELETE FROM \"{tableName.Replace(".", "\".\"")}\" WHERE {conditions}", cancellationToken),
cancellationToken);
}

public Task<int> DeleteAllAsync(Expression<Func<TEntity, bool>> where,
CancellationToken cancellationToken = default) =>
ExecuteDbContextOperationAsync(
context => context.Set<TEntity>().Where(where).ExecuteDeleteAsync(cancellationToken),
cancellationToken);

public Task<int> DeleteAllAsync(CancellationToken cancellationToken = default) =>
ExecuteDbContextOperationAsync(context => context.Set<TEntity>().ExecuteDeleteAsync(cancellationToken),
cancellationToken);

private EntityChange[] Compare(TEntity firstEntity, TEntity secondEntity)
{
var changes = new List<EntityChange>();
Expand Down Expand Up @@ -845,15 +870,6 @@ private static void AttachAllEntities(TDbContext context, IEntity entity, Entity
return Task.FromResult(entry.State == EntityState.Deleted);
}, cancellationToken);

public Task<int> DeleteAllRawAsync(string conditions, CancellationToken cancellationToken = default)
{
var tableName = dbContext.Model.FindEntityType(typeof(TEntity))?.GetSchemaQualifiedTableName() ??
throw new InvalidOperationException($"Can't find table name for entity {typeof(TEntity)}");
return ExecuteDbContextOperationAsync(context => context.Database.ExecuteSqlRawAsync(
$"DELETE FROM \"{tableName.Replace(".", "\".\"")}\" WHERE {conditions}", cancellationToken),
cancellationToken);
}

[PublicAPI]
protected IQueryable<TEntity> GetBaseQuery() => dbContext.Set<TEntity>().AsQueryable();

Expand Down Expand Up @@ -1030,4 +1046,3 @@ public enum ChangeState
UnChanged,
Unknown
}

37 changes: 36 additions & 1 deletion tests/Sitko.Core.Repository.EntityFrameworkCore.Tests/EFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,40 @@ public async Task DeleteAllRaw()
item = await repository.GetByIdAsync(item.Id);
item.Should().BeNull();
}
}

[Fact]
public async Task DeleteAll()
{
var scope = await GetScopeAsync();

var repository = scope.GetService<IRepository<FooModel, Guid>>();
Assert.NotNull(repository);
var items = await repository.CountAsync();
items.Should().BeGreaterThan(0);

var efRepository = repository as IEFRepository;
efRepository.Should().NotBeNull();
var deleted = await efRepository!.DeleteAllAsync();
deleted.Should().Be(items);
items = await repository.CountAsync();
items.Should().Be(0);
}

[Fact]
public async Task DeleteAllCondition()
{
var scope = await GetScopeAsync();

var repository = scope.GetService<IRepository<FooModel, Guid>>();
Assert.NotNull(repository);
var item = await repository.GetAsync();
Assert.NotNull(item);

var efRepository = repository as IEFRepository<FooModel>;
efRepository.Should().NotBeNull();
var deleted = await efRepository!.DeleteAllAsync(model => model.Id == item.Id);
deleted.Should().Be(1);
item = await repository.GetByIdAsync(item.Id);
item.Should().BeNull();
}
}

0 comments on commit f1306fd

Please sign in to comment.