|
|
|
@ -1,5 +1,7 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
|
@ -8,9 +10,9 @@ using Volo.Abp.EntityFrameworkCore; |
|
|
|
|
|
|
|
namespace Volo.Abp.Repositories.EntityFrameworkCore |
|
|
|
{ |
|
|
|
//TODO: Override async versions
|
|
|
|
//TODO: Override async versions and others
|
|
|
|
|
|
|
|
public class EfCoreRepository<TDbContext, TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey> |
|
|
|
public class EfCoreRepository<TDbContext, TEntity, TPrimaryKey> : QueryableRepositoryBase<TEntity, TPrimaryKey> |
|
|
|
where TDbContext : AbpDbContext<TDbContext> |
|
|
|
where TEntity : class, IEntity<TPrimaryKey> |
|
|
|
{ |
|
|
|
@ -23,19 +25,19 @@ namespace Volo.Abp.Repositories.EntityFrameworkCore |
|
|
|
DbContext = dbContext; |
|
|
|
} |
|
|
|
|
|
|
|
public override List<TEntity> GetAllList() |
|
|
|
public override IQueryable<TEntity> GetQueryable() |
|
|
|
{ |
|
|
|
return GetQueryable().ToList(); |
|
|
|
return DbSet; |
|
|
|
} |
|
|
|
|
|
|
|
public override Task<List<TEntity>> GetAllListAsync() |
|
|
|
public override Task<List<TEntity>> GetListAsync() |
|
|
|
{ |
|
|
|
return GetQueryable().ToListAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public override TEntity Get(TPrimaryKey id) |
|
|
|
public override async Task<TEntity> GetAsync(TPrimaryKey id) |
|
|
|
{ |
|
|
|
var entity = FirstOrDefault(id); |
|
|
|
var entity = await FirstOrDefaultAsync(id); |
|
|
|
if (entity == null) |
|
|
|
{ |
|
|
|
throw new EntityNotFoundException(typeof(TEntity), id); |
|
|
|
@ -49,40 +51,53 @@ namespace Volo.Abp.Repositories.EntityFrameworkCore |
|
|
|
return DbSet.Find(id); |
|
|
|
} |
|
|
|
|
|
|
|
public override Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id) |
|
|
|
{ |
|
|
|
return DbSet.FindAsync(id); |
|
|
|
} |
|
|
|
|
|
|
|
public override Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate) |
|
|
|
{ |
|
|
|
return GetQueryable().FirstOrDefaultAsync(predicate); |
|
|
|
} |
|
|
|
|
|
|
|
public override TEntity Insert(TEntity entity) |
|
|
|
{ |
|
|
|
return DbSet.Add(entity).Entity; |
|
|
|
} |
|
|
|
|
|
|
|
public override TEntity Update(TEntity entity) |
|
|
|
public override TPrimaryKey InsertAndGetId(TEntity entity) |
|
|
|
{ |
|
|
|
return DbSet.Update(entity).Entity; |
|
|
|
var insertedEntity = Insert(entity); |
|
|
|
DbContext.SaveChanges(); |
|
|
|
return insertedEntity.Id; |
|
|
|
} |
|
|
|
|
|
|
|
public override void Delete(TEntity entity) |
|
|
|
public override async Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity) |
|
|
|
{ |
|
|
|
DbSet.Remove(entity); |
|
|
|
var insertedEntity = await InsertAsync(entity); |
|
|
|
await DbContext.SaveChangesAsync(); |
|
|
|
return insertedEntity.Id; |
|
|
|
} |
|
|
|
|
|
|
|
public override void Delete(TPrimaryKey id) |
|
|
|
public override TEntity Update(TEntity entity) |
|
|
|
{ |
|
|
|
var entity = FirstOrDefault(id); |
|
|
|
if (entity == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
return DbSet.Update(entity).Entity; |
|
|
|
} |
|
|
|
|
|
|
|
Delete(entity); |
|
|
|
public override void Delete(TEntity entity) |
|
|
|
{ |
|
|
|
DbSet.Remove(entity); |
|
|
|
} |
|
|
|
|
|
|
|
public override int Count() |
|
|
|
public override Task<int> CountAsync() |
|
|
|
{ |
|
|
|
return GetQueryable().Count(); |
|
|
|
return DbSet.CountAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual IQueryable<TEntity> GetQueryable() |
|
|
|
public override Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate) |
|
|
|
{ |
|
|
|
return DbSet; |
|
|
|
return DbSet.CountAsync(predicate); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|