| | | 1 | | // ReSharper disable once CheckNamespace |
| | | 2 | | namespace Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | /// <summary>Write helpers on <see cref="DbContext"/>. The <c>*AndSave</c> methods save at once; exceptions are not caug |
| | | 5 | | public static class DbContextWriteExtensions |
| | | 6 | | { |
| | | 7 | | /// <summary>Adds <paramref name="entities"/> and saves.</summary> |
| | | 8 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 9 | | /// <param name="dbContext">The context.</param> |
| | | 10 | | /// <param name="entities">The entities.</param> |
| | | 11 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 12 | | public static void AddEntitiesAndSave< TEntity >( this DbContext dbContext, IEnumerable<TEntity> entities ) |
| | | 13 | | where TEntity : class |
| | | 14 | | { |
| | 0 | 15 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 16 | | |
| | 0 | 17 | | dbContext.Set<TEntity>().AddRange( entities ); |
| | 0 | 18 | | dbContext.SaveChanges(); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>Async version of <see cref="AddEntitiesAndSave{TEntity}"/>.</summary> |
| | | 22 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 23 | | /// <param name="dbContext">The context.</param> |
| | | 24 | | /// <param name="entities">The entities.</param> |
| | | 25 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 26 | | /// <returns>A task that completes when the save is done.</returns> |
| | | 27 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 28 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was cancelled.</exception> |
| | | 29 | | public static async Task AddEntitiesAndSaveAsync< TEntity >( this DbContext dbContext, IEnumerable<TEntity> entities |
| | | 30 | | where TEntity : class |
| | | 31 | | { |
| | 0 | 32 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 33 | | |
| | 0 | 34 | | await dbContext.Set<TEntity>().AddRangeAsync( entities, cancellationToken ).ConfigureAwait( false ); |
| | 0 | 35 | | await dbContext.SaveChangesAsync( cancellationToken ).ConfigureAwait( false ); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>Adds <paramref name="entity"/> and saves.</summary> |
| | | 39 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 40 | | /// <param name="dbContext">The context.</param> |
| | | 41 | | /// <param name="entity">The entity.</param> |
| | | 42 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 43 | | public static void AddEntityAndSave< TEntity >( this DbContext dbContext, TEntity entity ) |
| | | 44 | | where TEntity : class |
| | | 45 | | { |
| | 0 | 46 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 47 | | |
| | 0 | 48 | | dbContext.Set<TEntity>().Add( entity ); |
| | 0 | 49 | | dbContext.SaveChanges(); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Async version of <see cref="AddEntityAndSave{TEntity}"/>.</summary> |
| | | 53 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 54 | | /// <param name="dbContext">The context.</param> |
| | | 55 | | /// <param name="entity">The entity.</param> |
| | | 56 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 57 | | /// <returns>The entity's change-tracking entry.</returns> |
| | | 58 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 59 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was cancelled.</exception> |
| | | 60 | | public static async Task<EntityEntry<TEntity>> AddEntityAndSaveAsync< TEntity >( this DbContext dbContext, TEntity e |
| | | 61 | | where TEntity : class |
| | | 62 | | { |
| | 0 | 63 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 64 | | |
| | 0 | 65 | | var entry = await dbContext.Set<TEntity>().AddAsync( entity, cancellationToken ).ConfigureAwait( false ); |
| | 0 | 66 | | await dbContext.SaveChangesAsync( cancellationToken ).ConfigureAwait( false ); |
| | | 67 | | |
| | | 68 | | |
| | 0 | 69 | | return entry; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary>Marks <paramref name="entity"/> as deleted (an added entity is just detached). Nothing is saved.</summa |
| | | 73 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 74 | | /// <param name="dbContext">The context.</param> |
| | | 75 | | /// <param name="entity">The entity.</param> |
| | | 76 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 77 | | public static void DeleteEntity< TEntity >( this DbContext dbContext, TEntity entity ) |
| | | 78 | | where TEntity : class |
| | | 79 | | { |
| | 0 | 80 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 81 | | |
| | 0 | 82 | | dbContext.Set<TEntity>().Remove( entity ); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary>Deletes <paramref name="entity"/> and saves.</summary> |
| | | 86 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 87 | | /// <param name="dbContext">The context.</param> |
| | | 88 | | /// <param name="entity">The entity.</param> |
| | | 89 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 90 | | public static void DeleteEntityAndSave< TEntity >( this DbContext dbContext, TEntity entity ) |
| | | 91 | | where TEntity : class |
| | | 92 | | { |
| | 0 | 93 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 94 | | |
| | 0 | 95 | | dbContext.Set<TEntity>().Remove( entity ); |
| | 0 | 96 | | dbContext.SaveChanges(); |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary>Updates <paramref name="entities"/> and saves.</summary> |
| | | 100 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 101 | | /// <param name="dbContext">The context.</param> |
| | | 102 | | /// <param name="entities">The entities.</param> |
| | | 103 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 104 | | public static void UpdateEntitiesAndSave< TEntity >( this DbContext dbContext, IEnumerable<TEntity> entities ) |
| | | 105 | | where TEntity : class |
| | | 106 | | { |
| | 0 | 107 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 108 | | |
| | 0 | 109 | | dbContext.Set<TEntity>().UpdateRange( entities ); |
| | 0 | 110 | | dbContext.SaveChanges(); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary>Updates <paramref name="entity"/> and saves.</summary> |
| | | 114 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 115 | | /// <param name="dbContext">The context.</param> |
| | | 116 | | /// <param name="entity">The entity.</param> |
| | | 117 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 118 | | public static void UpdateEntityAndSave< TEntity >( this DbContext dbContext, TEntity entity ) |
| | | 119 | | where TEntity : class |
| | | 120 | | { |
| | 0 | 121 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 122 | | |
| | 0 | 123 | | dbContext.Set<TEntity>().Update( entity ); |
| | 0 | 124 | | dbContext.SaveChanges(); |
| | 0 | 125 | | } |
| | | 126 | | } |