|
|
|
@ -9,6 +9,7 @@ using System.Linq.Expressions; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.EntityFrameworkCore.Storage; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
|
using Volo.Abp.EntityFrameworkCore; |
|
|
|
@ -107,7 +108,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
CheckAndSetId(entity); |
|
|
|
|
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
@ -124,7 +125,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
var entityArray = entities.ToArray(); |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
@ -155,7 +156,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
|
|
|
|
dbContext.Attach(entity); |
|
|
|
@ -172,7 +173,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
|
|
|
|
if (BulkOperationProvider != null) |
|
|
|
@ -199,7 +200,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
|
|
|
|
dbContext.Set<TEntity>().Remove(entity); |
|
|
|
@ -212,7 +213,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
|
|
|
|
if (BulkOperationProvider != null) |
|
|
|
@ -276,12 +277,12 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
[Obsolete("Use GetQueryableAsync method.")] |
|
|
|
protected override IQueryable<TEntity> GetQueryable() |
|
|
|
{ |
|
|
|
return DbSet.AsQueryable().AsNoTrackingIf(IsReadOnly); |
|
|
|
return DbSet.AsQueryable().AsNoTrackingIf(!IsChangeTrackingEnabled); |
|
|
|
} |
|
|
|
|
|
|
|
public async override Task<IQueryable<TEntity>> GetQueryableAsync() |
|
|
|
{ |
|
|
|
return (await GetDbSetAsync()).AsQueryable().AsNoTrackingIf(IsReadOnly); |
|
|
|
return (await GetDbSetAsync()).AsQueryable().AsNoTrackingIf(!IsChangeTrackingEnabled); |
|
|
|
} |
|
|
|
|
|
|
|
protected async override Task SaveChangesAsync(CancellationToken cancellationToken) |
|
|
|
@ -305,7 +306,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
CheckChangeTracking(); |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
var dbSet = dbContext.Set<TEntity>(); |
|
|
|
|
|
|
|
@ -323,7 +324,6 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
|
|
|
|
public async override Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
var dbSet = dbContext.Set<TEntity>(); |
|
|
|
await dbSet.Where(predicate).ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); |
|
|
|
@ -428,18 +428,12 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void CheckReadOnly() |
|
|
|
|
|
|
|
protected virtual void CheckChangeTracking() |
|
|
|
{ |
|
|
|
if (IsReadOnly) |
|
|
|
if (!IsChangeTrackingEnabled) |
|
|
|
{ |
|
|
|
throw new AbpRepositoryIsReadOnlyException($"Can not call " + |
|
|
|
$"{nameof(InsertAsync)}, " + |
|
|
|
$"{nameof(InsertManyAsync)}, " + |
|
|
|
$"{nameof(UpdateAsync)}, " + |
|
|
|
$"{nameof(UpdateManyAsync)}, " + |
|
|
|
$"{nameof(DeleteAsync)}, " + |
|
|
|
$"{nameof(DeleteManyAsync)}, " + |
|
|
|
$"{nameof(DeleteDirectAsync)} methods on a read-only repository!"); |
|
|
|
Logger.LogWarning("This repository has disabled change tracking. Your changes may not be saved!"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -473,14 +467,13 @@ public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbC |
|
|
|
{ |
|
|
|
return includeDetails |
|
|
|
? await (await WithDetailsAsync()).OrderBy(e => e.Id).FirstOrDefaultAsync(e => e.Id.Equals(id), GetCancellationToken(cancellationToken)) |
|
|
|
: IsReadOnly |
|
|
|
: !IsChangeTrackingEnabled |
|
|
|
? await (await GetQueryableAsync()).OrderBy(e => e.Id).FirstOrDefaultAsync(e => e.Id.Equals(id), GetCancellationToken(cancellationToken)) |
|
|
|
: await (await GetDbSetAsync()).FindAsync(new object[] {id}, GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
var entity = await FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
if (entity == null) |
|
|
|
{ |
|
|
|
@ -492,7 +485,6 @@ public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbC |
|
|
|
|
|
|
|
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
CheckReadOnly(); |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
|
|
|
|
var entities = await (await GetDbSetAsync()).Where(x => ids.Contains(x.Id)).ToListAsync(cancellationToken); |
|
|
|
|