Browse Source

Make EfCoreRepository implement IAsyncEnumerable<TEntity>

Resolve #4806
pull/4807/head
maliming 6 years ago
parent
commit
4c1d2f8957
  1. 7
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  2. 24
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs

7
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -15,7 +15,7 @@ using Volo.Abp.Guids;
namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
{ {
public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IEfCoreRepository<TEntity> public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IEfCoreRepository<TEntity>, IAsyncEnumerable<TEntity>
where TDbContext : IEfCoreDbContext where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity where TEntity : class, IEntity
{ {
@ -195,6 +195,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return query; return query;
} }
public IAsyncEnumerator<TEntity> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
{
return DbSet.AsAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
}
protected virtual void CheckAndSetId(TEntity entity) protected virtual void CheckAndSetId(TEntity entity)
{ {
if (entity is IEntity<Guid> entityWithGuidId) if (entity is IEntity<Guid> entityWithGuidId)

24
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs

@ -1,9 +1,31 @@
using Volo.Abp.TestApp.Testing; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Volo.Abp.TestApp.Testing;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore.Repositories namespace Volo.Abp.EntityFrameworkCore.Repositories
{ {
public class Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule> public class Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule>
{ {
[Fact]
public async Task EFCore_QueryableExtension_ToListAsync()
{
await WithUnitOfWorkAsync(async () =>
{
var persons = await PersonRepository.ToListAsync();
persons.Count.ShouldBeGreaterThan(0);
});
}
[Fact]
public async Task EFCore_QueryableExtension_CountAsync()
{
await WithUnitOfWorkAsync(async () =>
{
var count = await PersonRepository.CountAsync();
count.ShouldBeGreaterThan(0);
});
}
} }
} }

Loading…
Cancel
Save