| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Common.Multitenancy; |
| | | 5 | | using Elsa.Persistence.EFCore.Extensions; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Open.Linq.AsyncExtensions; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Persistence.EFCore; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A generic repository class around EF Core for accessing entities. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="TDbContext">The type of the database context.</typeparam> |
| | | 18 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 19 | | [PublicAPI] |
| | 2959 | 20 | | public class Store<TDbContext, TEntity>(IDbContextFactory<TDbContext> dbContextFactory, IServiceProvider serviceProvider |
| | | 21 | | { |
| | | 22 | | // ReSharper disable once StaticMemberInGenericType |
| | | 23 | | // Justification: This is a static member that is used to ensure that only one thread can access the database for TE |
| | 3 | 24 | | private static readonly SemaphoreSlim Semaphore = new(1, 1); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates a new instance of the database context. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 30 | | /// <returns>The database context.</returns> |
| | 5993 | 31 | | public async Task<TDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default) => await dbContext |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Adds the specified entity. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="entity">The entity to add.</param> |
| | | 37 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 38 | | public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 39 | | { |
| | 0 | 40 | | await AddAsync(entity, null, cancellationToken); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Adds the specified entity. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="entity">The entity to add.</param> |
| | | 47 | | /// <param name="onAdding">The callback to invoke before adding the entity.</param> |
| | | 48 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 49 | | public async Task AddAsync(TEntity entity, Func<TDbContext, TEntity, CancellationToken, ValueTask>? onAdding, Cancel |
| | | 50 | | { |
| | 48 | 51 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 52 | | |
| | 48 | 53 | | if (onAdding != null) |
| | 48 | 54 | | await onAdding(dbContext, entity, cancellationToken); |
| | | 55 | | |
| | 48 | 56 | | var set = dbContext.Set<TEntity>(); |
| | 48 | 57 | | await set.AddAsync(entity, cancellationToken); |
| | 48 | 58 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 48 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Adds the specified entities. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="entities">The entities to save.</param> |
| | | 65 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 66 | | public async Task AddManyAsync( |
| | | 67 | | IEnumerable<TEntity> entities, |
| | | 68 | | CancellationToken cancellationToken = default) |
| | | 69 | | { |
| | 0 | 70 | | await AddManyAsync(entities, null, cancellationToken); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Adds the specified entities. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="entities">The entities to save.</param> |
| | | 77 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 78 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 79 | | public async Task AddManyAsync( |
| | | 80 | | IEnumerable<TEntity> entities, |
| | | 81 | | Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving = null, |
| | | 82 | | CancellationToken cancellationToken = default) |
| | | 83 | | { |
| | 323 | 84 | | var entityList = entities.ToList(); |
| | | 85 | | |
| | 323 | 86 | | if (entityList.Count == 0) |
| | 0 | 87 | | return; |
| | | 88 | | |
| | 323 | 89 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 90 | | |
| | 323 | 91 | | if (onSaving != null) |
| | | 92 | | { |
| | 5092 | 93 | | var savingTasks = entityList.Select(entity => onSaving(dbContext, entity, cancellationToken).AsTask()).ToLis |
| | 323 | 94 | | await Task.WhenAll(savingTasks); |
| | | 95 | | } |
| | | 96 | | |
| | 323 | 97 | | await dbContext.BulkInsertAsync(entityList, cancellationToken); |
| | 323 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Saves the entity. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="entity">The entity to save.</param> |
| | | 104 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 105 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 0 | 106 | | public async Task SaveAsync(TEntity entity, Expression<Func<TEntity, string>> keySelector, CancellationToken cancell |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Saves the entity. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="entity">The entity to save.</param> |
| | | 112 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 113 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 114 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 115 | | public async Task SaveAsync(TEntity entity, Expression<Func<TEntity, string>> keySelector, Func<TDbContext, TEntity, |
| | | 116 | | { |
| | 396 | 117 | | await Semaphore.WaitAsync(cancellationToken); // Asynchronous wait |
| | | 118 | | |
| | | 119 | | try |
| | | 120 | | { |
| | 396 | 121 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 122 | | |
| | 396 | 123 | | if (onSaving != null) |
| | 396 | 124 | | await onSaving(dbContext, entity, cancellationToken); |
| | | 125 | | |
| | 396 | 126 | | var set = dbContext.Set<TEntity>(); |
| | 396 | 127 | | var lambda = keySelector.BuildEqualsExpression(entity); |
| | 396 | 128 | | var exists = await set.AnyAsync(lambda, cancellationToken); |
| | 396 | 129 | | set.Entry(entity).State = exists ? EntityState.Modified : EntityState.Added; |
| | 396 | 130 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 396 | 131 | | } |
| | 0 | 132 | | catch (Exception ex) |
| | | 133 | | { |
| | 0 | 134 | | var handler = serviceProvider.GetService<IDbExceptionHandler>(); |
| | | 135 | | |
| | 0 | 136 | | if (handler != null) |
| | | 137 | | { |
| | 0 | 138 | | var context = new DbUpdateExceptionContext(ex, cancellationToken); |
| | 0 | 139 | | await handler.HandleAsync(context); |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | throw; |
| | | 143 | | } |
| | | 144 | | finally |
| | | 145 | | { |
| | 396 | 146 | | Semaphore.Release(); |
| | | 147 | | } |
| | 396 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Saves the specified entities. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="entities">The entities to save.</param> |
| | | 154 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 155 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 0 | 156 | | public async Task SaveManyAsync(IEnumerable<TEntity> entities, Expression<Func<TEntity, string>> keySelector, Cancel |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Saves the specified entities. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="entities">The entities to save.</param> |
| | | 162 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 163 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 164 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 165 | | public async Task SaveManyAsync( |
| | | 166 | | IEnumerable<TEntity> entities, |
| | | 167 | | Expression<Func<TEntity, string>> keySelector, |
| | | 168 | | Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving = null, |
| | | 169 | | CancellationToken cancellationToken = default) |
| | | 170 | | { |
| | 1385 | 171 | | var entityList = entities.ToList(); |
| | | 172 | | |
| | 1385 | 173 | | if (entityList.Count == 0) |
| | 475 | 174 | | return; |
| | | 175 | | |
| | 910 | 176 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 177 | | |
| | 910 | 178 | | if (onSaving != null) |
| | | 179 | | { |
| | 3981 | 180 | | var savingTasks = entityList.Select(entity => onSaving(dbContext, entity, cancellationToken).AsTask()).ToLis |
| | 910 | 181 | | await Task.WhenAll(savingTasks); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | // When doing a custom SQL query (Bulk Upsert), none of the installed query filters will be applied. Hence, we a |
| | 910 | 185 | | var tenantId = serviceProvider.GetRequiredService<ITenantAccessor>().Tenant?.Id.NullIfEmpty(); |
| | 7962 | 186 | | foreach (var entity in entityList) |
| | | 187 | | { |
| | 3071 | 188 | | if (entity is Entity entityWithTenant) |
| | 3071 | 189 | | entityWithTenant.TenantId = tenantId; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | try |
| | | 193 | | { |
| | 910 | 194 | | await dbContext.BulkUpsertAsync(entityList, keySelector, cancellationToken); |
| | 910 | 195 | | } |
| | 0 | 196 | | catch (Exception ex) |
| | | 197 | | { |
| | 0 | 198 | | var handler = serviceProvider.GetService<IDbExceptionHandler>(); |
| | | 199 | | |
| | 0 | 200 | | if (handler != null) |
| | | 201 | | { |
| | 0 | 202 | | var context = new DbUpdateExceptionContext(ex, cancellationToken); |
| | 0 | 203 | | await handler.HandleAsync(context); |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | throw; |
| | | 207 | | } |
| | 1385 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Updates the entity. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="entity">The entity to update.</param> |
| | | 214 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 215 | | public Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 216 | | { |
| | 0 | 217 | | return UpdateAsync(entity, null, cancellationToken); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Updates the entity. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <param name="entity">The entity to update.</param> |
| | | 224 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 225 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 226 | | public async Task UpdateAsync(TEntity entity, Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving, Can |
| | | 227 | | { |
| | 0 | 228 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 229 | | |
| | 0 | 230 | | if (onSaving != null) |
| | 0 | 231 | | await onSaving(dbContext, entity, cancellationToken); |
| | | 232 | | |
| | 0 | 233 | | var set = dbContext.Set<TEntity>(); |
| | 0 | 234 | | set.Entry(entity).State = EntityState.Modified; |
| | 0 | 235 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Updates specific properties of an entity in the database. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <param name="entity">The entity to update.</param> |
| | | 242 | | /// <param name="properties">An array of expressions indicating the properties to update.</param> |
| | | 243 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 244 | | /// <returns>A task that represents the asynchronous operation.</returns> |
| | | 245 | | public async Task UpdatePartialAsync(TEntity entity, Expression<Func<TEntity, object>>[] properties, CancellationTok |
| | | 246 | | { |
| | 0 | 247 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 248 | | dbContext.Attach(entity); |
| | | 249 | | |
| | 0 | 250 | | foreach (var property in properties) |
| | 0 | 251 | | dbContext.Entry(entity).Property(property).IsModified = true; |
| | | 252 | | |
| | 0 | 253 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Finds the entity matching the specified predicate. |
| | | 258 | | /// </summary> |
| | | 259 | | /// <param name="predicate">The predicate to use.</param> |
| | | 260 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 261 | | /// <returns>The entity if found, otherwise <c>null</c>.</returns> |
| | 0 | 262 | | public async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Finds the entity matching the specified predicate. |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="predicate">The predicate to use.</param> |
| | | 268 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 269 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 270 | | /// <returns></returns> |
| | | 271 | | public async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, Func<TDbContext, TEntity?, TEntity? |
| | | 272 | | { |
| | 0 | 273 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 274 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 275 | | var entity = await set.FirstOrDefaultAsync(predicate, cancellationToken); |
| | | 276 | | |
| | 0 | 277 | | if (entity == null) |
| | 0 | 278 | | return null; |
| | | 279 | | |
| | 0 | 280 | | if (onLoading != null) |
| | 0 | 281 | | entity = onLoading.Invoke(dbContext, entity); |
| | | 282 | | |
| | 0 | 283 | | return entity; |
| | 0 | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Finds a single entity using a query |
| | | 288 | | /// </summary> |
| | | 289 | | /// <param name="query">The query to use</param> |
| | | 290 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 291 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 292 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 293 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbContext, TEntity |
| | | 294 | | { |
| | 0 | 295 | | return await FindAsync(query, onLoading, false, cancellationToken); |
| | 0 | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Finds a single entity using a query |
| | | 300 | | /// </summary> |
| | | 301 | | /// <param name="query">The query to use</param> |
| | | 302 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 303 | | /// <param name="tenantAgnostic">Define is the request should be tenant agnostic or not</param> |
| | | 304 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 305 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 306 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbContext, TEntity |
| | | 307 | | { |
| | 2 | 308 | | return await QueryAsync(query, onLoading, tenantAgnostic, cancellationToken).FirstOrDefault(); |
| | 2 | 309 | | } |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Finds a single entity using a query |
| | | 313 | | /// </summary> |
| | | 314 | | /// <param name="query">The query to use</param> |
| | | 315 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 316 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 317 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel |
| | | 318 | | { |
| | 0 | 319 | | return await FindAsync(query, false, cancellationToken); |
| | 0 | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Finds a single entity using a query |
| | | 324 | | /// </summary> |
| | | 325 | | /// <param name="query">The query to use</param> |
| | | 326 | | /// <param name="tenantAgnostic">Define is the request should be tenant agnostic or not</param> |
| | | 327 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 328 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 329 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool tenantAgnostic = fa |
| | | 330 | | { |
| | 0 | 331 | | return await QueryAsync(query, tenantAgnostic, cancellationToken).FirstOrDefault(); |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Finds a list of entities using a query |
| | | 336 | | /// </summary> |
| | 0 | 337 | | public async Task<IEnumerable<TEntity>> FindManyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken c |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// Finds a list of entities using a query |
| | | 341 | | /// </summary> |
| | | 342 | | public async Task<IEnumerable<TEntity>> FindManyAsync(Expression<Func<TEntity, bool>> predicate, Action<TDbContext, |
| | | 343 | | { |
| | 0 | 344 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 345 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 346 | | var entities = await set.Where(predicate).ToListAsync(cancellationToken); |
| | | 347 | | |
| | 0 | 348 | | if (onLoading != null) |
| | 0 | 349 | | foreach (var entity in entities) |
| | 0 | 350 | | onLoading(dbContext, entity); |
| | | 351 | | |
| | 0 | 352 | | return entities; |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <summary> |
| | | 356 | | /// Finds a list of entities using a query |
| | | 357 | | /// </summary> |
| | | 358 | | public async Task<Page<TEntity>> FindManyAsync<TKey>( |
| | | 359 | | Expression<Func<TEntity, bool>> predicate, |
| | | 360 | | Expression<Func<TEntity, TKey>> orderBy, |
| | | 361 | | OrderDirection orderDirection = OrderDirection.Ascending, |
| | | 362 | | PageArgs? pageArgs = null, |
| | | 363 | | CancellationToken cancellationToken = default) => |
| | 0 | 364 | | await FindManyAsync(predicate, orderBy, orderDirection, pageArgs, null, cancellationToken); |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Returns a list of entities using a query |
| | | 368 | | /// </summary> |
| | | 369 | | public async Task<Page<TEntity>> FindManyAsync<TKey>( |
| | | 370 | | Expression<Func<TEntity, bool>>? predicate, |
| | | 371 | | Expression<Func<TEntity, TKey>>? orderBy, |
| | | 372 | | OrderDirection orderDirection = OrderDirection.Ascending, |
| | | 373 | | PageArgs? pageArgs = null, |
| | | 374 | | Func<TDbContext, TEntity?, TEntity?>? onLoading = null, |
| | | 375 | | CancellationToken cancellationToken = default) |
| | | 376 | | { |
| | 0 | 377 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 378 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 379 | | |
| | 0 | 380 | | if (predicate != null) |
| | 0 | 381 | | set = set.Where(predicate); |
| | | 382 | | |
| | 0 | 383 | | if (orderBy != null) |
| | 0 | 384 | | set = orderDirection switch |
| | 0 | 385 | | { |
| | 0 | 386 | | OrderDirection.Ascending => set.OrderBy(orderBy), |
| | 0 | 387 | | OrderDirection.Descending => set.OrderByDescending(orderBy), |
| | 0 | 388 | | _ => set.OrderBy(orderBy) |
| | 0 | 389 | | }; |
| | | 390 | | |
| | 0 | 391 | | var page = await set.PaginateAsync(pageArgs); |
| | | 392 | | |
| | 0 | 393 | | if (onLoading != null) |
| | 0 | 394 | | page = page with |
| | 0 | 395 | | { |
| | 0 | 396 | | Items = page.Items.Select(x => onLoading(dbContext, x)!).ToList() |
| | 0 | 397 | | }; |
| | | 398 | | |
| | 0 | 399 | | return page; |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | public Task<IEnumerable<TEntity>> ListAsync(CancellationToken cancellationToken = default) |
| | | 403 | | { |
| | 0 | 404 | | return ListAsync(null, cancellationToken); |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | public async Task<IEnumerable<TEntity>> ListAsync(Action<TDbContext, TEntity?>? onLoading = null, CancellationToken |
| | | 408 | | { |
| | 0 | 409 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 410 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 411 | | var entities = await set.ToListAsync(cancellationToken); |
| | | 412 | | |
| | 0 | 413 | | if (onLoading != null) |
| | 0 | 414 | | foreach (var entity in entities) |
| | 0 | 415 | | onLoading(dbContext, entity); |
| | | 416 | | |
| | 0 | 417 | | return entities; |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Finds a single entity using a query. |
| | | 422 | | /// </summary> |
| | | 423 | | /// <returns>True if the entity was found, otherwise false.</returns> |
| | | 424 | | public async Task<bool> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 425 | | { |
| | 0 | 426 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 427 | | var set = dbContext.Set<TEntity>(); |
| | 0 | 428 | | set.Attach(entity).State = EntityState.Deleted; |
| | 0 | 429 | | return await dbContext.SaveChangesAsync(cancellationToken) == 1; |
| | 0 | 430 | | } |
| | | 431 | | |
| | | 432 | | /// <summary> |
| | | 433 | | /// Deletes entities using a predicate. |
| | | 434 | | /// </summary> |
| | | 435 | | /// <returns>The number of entities deleted.</returns> |
| | | 436 | | public async Task<long> DeleteWhereAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationTo |
| | | 437 | | { |
| | 4 | 438 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 4 | 439 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 4 | 440 | | return await set.Where(predicate).ExecuteDeleteAsync(cancellationToken); |
| | 4 | 441 | | } |
| | | 442 | | |
| | | 443 | | /// <summary> |
| | | 444 | | /// Deletes entities using a query. |
| | | 445 | | /// </summary> |
| | | 446 | | /// <returns>The number of entities deleted.</returns> |
| | | 447 | | public async Task<long> DeleteWhereAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken can |
| | | 448 | | { |
| | 99 | 449 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 99 | 450 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 99 | 451 | | var queryable = query(set.AsQueryable()); |
| | 99 | 452 | | return await queryable.ExecuteDeleteAsync(cancellationToken); |
| | 99 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Queries the database using a query. |
| | | 457 | | /// </summary> |
| | | 458 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Cancellatio |
| | | 459 | | { |
| | 47 | 460 | | return await QueryAsync(query, null, false, cancellationToken); |
| | 47 | 461 | | } |
| | | 462 | | |
| | | 463 | | /// <summary> |
| | | 464 | | /// Queries the database using a query. |
| | | 465 | | /// </summary> |
| | | 466 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool tenant |
| | | 467 | | { |
| | 0 | 468 | | return await QueryAsync(query, null, tenantAgnostic, cancellationToken); |
| | 0 | 469 | | } |
| | | 470 | | |
| | | 471 | | /// <summary> |
| | | 472 | | /// Queries the database using a query and a selector. |
| | | 473 | | /// </summary> |
| | | 474 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbCon |
| | | 475 | | { |
| | 167 | 476 | | return await QueryAsync(query, onLoading, false, cancellationToken); |
| | 167 | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary> |
| | | 480 | | /// Queries the database using a query and a selector. |
| | | 481 | | /// </summary> |
| | | 482 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbCon |
| | | 483 | | { |
| | 4197 | 484 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 4197 | 485 | | var asNoTracking = onLoading == null; |
| | 4197 | 486 | | var set = asNoTracking ? dbContext.Set<TEntity>().AsNoTracking() : dbContext.Set<TEntity>(); |
| | 4197 | 487 | | var queryable = query(set.AsQueryable()); |
| | | 488 | | |
| | 4197 | 489 | | if (ignoreQueryFilters) |
| | 3 | 490 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 491 | | |
| | 4197 | 492 | | var entities = await queryable.ToListAsync(cancellationToken); |
| | | 493 | | |
| | 4197 | 494 | | if (onLoading != null) |
| | | 495 | | { |
| | 7713 | 496 | | var loadingTasks = entities.Select(entity => onLoading(dbContext, entity, cancellationToken).AsTask()).ToLis |
| | 4150 | 497 | | await Task.WhenAll(loadingTasks); |
| | | 498 | | } |
| | | 499 | | |
| | 4197 | 500 | | return entities; |
| | 4197 | 501 | | } |
| | | 502 | | |
| | | 503 | | /// <summary> |
| | | 504 | | /// Queries the database using a query and a selector. |
| | | 505 | | /// </summary> |
| | | 506 | | public async Task<IEnumerable<TResult>> QueryAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Ex |
| | | 507 | | { |
| | 8 | 508 | | return await QueryAsync(query, selector, false, cancellationToken); |
| | 8 | 509 | | } |
| | | 510 | | |
| | | 511 | | /// <summary> |
| | | 512 | | /// Queries the database using a query and a selector. |
| | | 513 | | /// </summary> |
| | | 514 | | public async Task<IEnumerable<TResult>> QueryAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Ex |
| | | 515 | | { |
| | 8 | 516 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 8 | 517 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 8 | 518 | | var queryable = query(set.AsQueryable()); |
| | | 519 | | |
| | 8 | 520 | | if (ignoreQueryFilters) |
| | 0 | 521 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 522 | | |
| | 8 | 523 | | queryable = query(queryable); |
| | 8 | 524 | | return await queryable.Select(selector).ToListAsync(cancellationToken); |
| | 8 | 525 | | } |
| | | 526 | | |
| | | 527 | | /// <summary> |
| | | 528 | | /// Counts the number of entities matching a query. |
| | | 529 | | /// </summary> |
| | | 530 | | public async Task<long> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancellat |
| | | 531 | | { |
| | 0 | 532 | | return await CountAsync(query, false, cancellationToken); |
| | 0 | 533 | | } |
| | | 534 | | |
| | | 535 | | /// <summary> |
| | | 536 | | /// Counts the number of entities matching a query. |
| | | 537 | | /// </summary> |
| | | 538 | | public async Task<long> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool ignoreQueryFilters = f |
| | | 539 | | { |
| | 0 | 540 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 541 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 542 | | var queryable = query(set.AsQueryable()); |
| | | 543 | | |
| | 0 | 544 | | if (ignoreQueryFilters) |
| | 0 | 545 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 546 | | |
| | 0 | 547 | | queryable = query(queryable); |
| | 0 | 548 | | return await queryable.LongCountAsync(cancellationToken: cancellationToken); |
| | 0 | 549 | | } |
| | | 550 | | |
| | | 551 | | /// <summary> |
| | | 552 | | /// Checks if any entities exist. |
| | | 553 | | /// </summary> |
| | | 554 | | public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = de |
| | | 555 | | { |
| | 0 | 556 | | return await AnyAsync(predicate, false, cancellationToken); |
| | 0 | 557 | | } |
| | | 558 | | |
| | | 559 | | /// <summary> |
| | | 560 | | /// Checks if any entities exist. |
| | | 561 | | /// </summary> |
| | | 562 | | public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, bool ignoreQueryFilters = false, Cancell |
| | | 563 | | { |
| | 0 | 564 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 565 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 566 | | return await set.AnyAsync(predicate, cancellationToken); |
| | 0 | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Counts the number of entities matching a predicate. |
| | | 571 | | /// </summary> |
| | | 572 | | /// <param name="predicate">The predicate.</param> |
| | | 573 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 574 | | public async Task<long> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = |
| | | 575 | | { |
| | 0 | 576 | | return await CountAsync(predicate, false, cancellationToken); |
| | 0 | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Counts the number of entities matching a predicate. |
| | | 581 | | /// </summary> |
| | | 582 | | /// <param name="predicate">The predicate.</param> |
| | | 583 | | /// <param name="ignoreQueryFilters">Whether to ignore query filters.</param> |
| | | 584 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 585 | | public async Task<long> CountAsync(Expression<Func<TEntity, bool>> predicate, bool ignoreQueryFilters = false, Cance |
| | | 586 | | { |
| | 0 | 587 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 588 | | var queryable = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 589 | | |
| | 0 | 590 | | if (ignoreQueryFilters) |
| | 0 | 591 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 592 | | |
| | 0 | 593 | | return await queryable.CountAsync(predicate, cancellationToken); |
| | 0 | 594 | | } |
| | | 595 | | |
| | | 596 | | /// <summary> |
| | | 597 | | /// Counts the distinct number of entities matching a predicate. |
| | | 598 | | /// </summary> |
| | | 599 | | /// <param name="predicate">The predicate.</param> |
| | | 600 | | /// <param name="propertySelector">The property selector to distinct by.</param> |
| | | 601 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 602 | | public async Task<long> CountAsync<TProperty>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TP |
| | | 603 | | { |
| | 0 | 604 | | return await CountAsync(predicate, propertySelector, false, cancellationToken); |
| | 0 | 605 | | } |
| | | 606 | | |
| | | 607 | | /// <summary> |
| | | 608 | | /// Counts the distinct number of entities matching a predicate. |
| | | 609 | | /// </summary> |
| | | 610 | | /// <param name="predicate">The predicate.</param> |
| | | 611 | | /// <param name="propertySelector">The property selector to distinct by.</param> |
| | | 612 | | /// <param name="ignoreQueryFilters">Whether to ignore query filters.</param> |
| | | 613 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 614 | | public async Task<long> CountAsync<TProperty>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TP |
| | | 615 | | { |
| | 0 | 616 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 617 | | var queryable = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 618 | | |
| | 0 | 619 | | if (ignoreQueryFilters) |
| | 0 | 620 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 621 | | |
| | 0 | 622 | | return await queryable |
| | 0 | 623 | | .Where(predicate) |
| | 0 | 624 | | .Select(propertySelector) |
| | 0 | 625 | | .Distinct() |
| | 0 | 626 | | .CountAsync(cancellationToken); |
| | 0 | 627 | | } |
| | | 628 | | } |