| | | 1 | | using ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | // ReSharper disable once CheckNamespace |
| | | 4 | | namespace Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Read helpers on <see cref="DbContext"/>. Reads are no-tracking unless the name ends in <c>Tracking</c>; |
| | | 8 | | /// <c>includes</c> are eager-loaded navigations. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class DbContextReadExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary>Counts the rows of <typeparamref name="TEntity"/>.</summary> |
| | | 13 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 14 | | /// <param name="dbContext">The context.</param> |
| | | 15 | | /// <returns>The row count.</returns> |
| | | 16 | | public static int Count< TEntity >( this DbContext dbContext ) |
| | | 17 | | where TEntity : class |
| | 0 | 18 | | => dbContext.Query<TEntity>( tracking: false ).Count(); |
| | | 19 | | |
| | | 20 | | /// <summary>Async version of <see cref="Count{TEntity}"/>.</summary> |
| | | 21 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 22 | | /// <param name="dbContext">The context.</param> |
| | | 23 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 24 | | /// <returns>The row count.</returns> |
| | | 25 | | public static Task<int> CountAsync< TEntity >( this DbContext dbContext, CancellationToken cancellationToken = defau |
| | | 26 | | where TEntity : class |
| | 0 | 27 | | => dbContext.Query<TEntity>( tracking: false ).CountAsync( cancellationToken ); |
| | | 28 | | |
| | | 29 | | /// <summary>Counts the rows matching <paramref name="predicate"/>.</summary> |
| | | 30 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 31 | | /// <param name="dbContext">The context.</param> |
| | | 32 | | /// <param name="predicate">The filter.</param> |
| | | 33 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 34 | | /// <param name="includes">Navigations to include.</param> |
| | | 35 | | /// <returns>The matching row count.</returns> |
| | | 36 | | public static Task<int> CountByAsync< TEntity >( |
| | | 37 | | this DbContext dbContext, Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = d |
| | | 38 | | params Expression<Func<TEntity, object>>[] includes |
| | | 39 | | ) |
| | | 40 | | where TEntity : class |
| | 0 | 41 | | => dbContext.Query( tracking: false, null, includes ).Where( predicate ).CountAsync( cancellationToken ); |
| | | 42 | | |
| | | 43 | | /// <summary>Returns the rows matching <paramref name="predicate"/>.</summary> |
| | | 44 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 45 | | /// <param name="dbContext">The context.</param> |
| | | 46 | | /// <param name="predicate">The filter.</param> |
| | | 47 | | /// <param name="includes">Navigations to include.</param> |
| | | 48 | | /// <returns>The rows.</returns> |
| | | 49 | | public static IReadOnlyList<TEntity> FindBy< TEntity >( this DbContext dbContext, Expression<Func<TEntity, bool>> pr |
| | | 50 | | where TEntity : class |
| | 0 | 51 | | => dbContext.Query( tracking: false, null, includes ).Where( predicate ).ToList(); |
| | | 52 | | |
| | | 53 | | /// <summary>Async version of <see cref="FindBy{TEntity}"/>.</summary> |
| | | 54 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 55 | | /// <param name="dbContext">The context.</param> |
| | | 56 | | /// <param name="predicate">The filter.</param> |
| | | 57 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 58 | | /// <param name="includes">Navigations to include.</param> |
| | | 59 | | /// <returns>The rows.</returns> |
| | | 60 | | public static async Task<IReadOnlyList<TEntity>> FindByAsync< TEntity >( |
| | | 61 | | this DbContext dbContext, Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = d |
| | | 62 | | params Expression<Func<TEntity, object>>[] includes |
| | | 63 | | ) |
| | | 64 | | where TEntity : class |
| | 0 | 65 | | => await dbContext.Query( tracking: false, null, includes ).Where( predicate ).ToListAsync( cancellationToken ). |
| | | 66 | | |
| | | 67 | | /// <summary>Like <see cref="FindByAsync{TEntity}"/>, with string include paths as well.</summary> |
| | | 68 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 69 | | /// <param name="dbContext">The context.</param> |
| | | 70 | | /// <param name="predicate">The filter.</param> |
| | | 71 | | /// <param name="includeStrings">Dotted navigation paths to include, e.g. <c>"Orders.Lines"</c>.</param> |
| | | 72 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 73 | | /// <param name="includes">Navigations to include.</param> |
| | | 74 | | /// <returns>The rows.</returns> |
| | | 75 | | public static async Task<IReadOnlyList<TEntity>> FindByIncAsync< TEntity >( |
| | | 76 | | this DbContext dbContext, Expression<Func<TEntity, bool>> predicate, string[]? includeStrings = null, Cancel |
| | | 77 | | params Expression<Func<TEntity, object>>[] includes |
| | | 78 | | ) |
| | | 79 | | where TEntity : class |
| | 0 | 80 | | => await dbContext.Query( tracking: false, includeStrings, includes ).Where( predicate ).ToListAsync( cancellati |
| | | 81 | | |
| | | 82 | | /// <summary>Returns the entity with key <paramref name="id"/>, or <see langword="null"/>.</summary> |
| | | 83 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 84 | | /// <typeparam name="TId">The key type.</typeparam> |
| | | 85 | | /// <param name="dbContext">The context.</param> |
| | | 86 | | /// <param name="id">The key.</param> |
| | | 87 | | /// <param name="includes">Navigations to include.</param> |
| | | 88 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 89 | | public static TEntity? Get< TEntity, TId >( this DbContext dbContext, TId id, params Expression<Func<TEntity, object |
| | | 90 | | where TEntity : class, IBaseEntity<TId> |
| | | 91 | | where TId : struct, IEquatable<TId> |
| | 0 | 92 | | => dbContext.Query( tracking: false, null, includes ).FirstOrDefault( t => t.Id.Equals( id ) ); |
| | | 93 | | |
| | | 94 | | /// <summary><see cref="Get{TEntity, TId}"/> for <see cref="Guid"/> keys.</summary> |
| | | 95 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 96 | | /// <param name="dbContext">The context.</param> |
| | | 97 | | /// <param name="id">The key.</param> |
| | | 98 | | /// <param name="includes">Navigations to include.</param> |
| | | 99 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 100 | | public static TEntity? Get< TEntity >( this DbContext dbContext, Guid id, params Expression<Func<TEntity, object>>[] |
| | | 101 | | where TEntity : class, IBaseGuidEntity |
| | 0 | 102 | | => dbContext.Get<TEntity, Guid>( id, includes ); |
| | | 103 | | |
| | | 104 | | /// <summary><see cref="Get{TEntity, TId}"/> for <see cref="int"/> keys.</summary> |
| | | 105 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 106 | | /// <param name="dbContext">The context.</param> |
| | | 107 | | /// <param name="id">The key.</param> |
| | | 108 | | /// <param name="includes">Navigations to include.</param> |
| | | 109 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 110 | | public static TEntity? Get< TEntity >( this DbContext dbContext, int id, params Expression<Func<TEntity, object>>[] |
| | | 111 | | where TEntity : class, IBaseEntity |
| | 0 | 112 | | => dbContext.Get<TEntity, int>( id, includes ); |
| | | 113 | | |
| | | 114 | | /// <summary>Returns every row of <typeparamref name="TEntity"/>.</summary> |
| | | 115 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 116 | | /// <param name="dbContext">The context.</param> |
| | | 117 | | /// <param name="includes">Navigations to include.</param> |
| | | 118 | | /// <returns>The rows.</returns> |
| | | 119 | | public static IReadOnlyList<TEntity> GetAll< TEntity >( this DbContext dbContext, params Expression<Func<TEntity, ob |
| | | 120 | | where TEntity : class |
| | 0 | 121 | | => dbContext.Query( tracking: false, null, includes ).ToList(); |
| | | 122 | | |
| | | 123 | | /// <summary>Async version of <see cref="GetAll{TEntity}"/>.</summary> |
| | | 124 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 125 | | /// <param name="dbContext">The context.</param> |
| | | 126 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 127 | | /// <param name="includes">Navigations to include.</param> |
| | | 128 | | /// <returns>The rows.</returns> |
| | | 129 | | public static async Task<IReadOnlyList<TEntity>> GetAllAsync< TEntity >( |
| | | 130 | | this DbContext dbContext, CancellationToken cancellationToken = default, params Expression<Func<TEntity, obj |
| | | 131 | | ) |
| | | 132 | | where TEntity : class |
| | 0 | 133 | | => await dbContext.Query( tracking: false, null, includes ).ToListAsync( cancellationToken ).ConfigureAwait( fal |
| | | 134 | | |
| | | 135 | | /// <summary>Async version of <see cref="Get{TEntity, TId}"/>.</summary> |
| | | 136 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 137 | | /// <typeparam name="TId">The key type.</typeparam> |
| | | 138 | | /// <param name="dbContext">The context.</param> |
| | | 139 | | /// <param name="id">The key.</param> |
| | | 140 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 141 | | /// <param name="includes">Navigations to include.</param> |
| | | 142 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 143 | | public static Task<TEntity?> GetAsync< TEntity, TId >( |
| | | 144 | | this DbContext dbContext, TId id, CancellationToken cancellationToken = default, params Expression<Func<TEnt |
| | | 145 | | ) |
| | | 146 | | where TEntity : class, IBaseEntity<TId> |
| | | 147 | | where TId : struct, IEquatable<TId> |
| | 0 | 148 | | => dbContext.Query( tracking: false, null, includes ).FirstOrDefaultAsync( t => t.Id.Equals( id ), cancellationT |
| | | 149 | | |
| | | 150 | | /// <summary><see cref="GetAsync{TEntity, TId}"/> for <see cref="int"/> keys.</summary> |
| | | 151 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 152 | | /// <param name="dbContext">The context.</param> |
| | | 153 | | /// <param name="id">The key.</param> |
| | | 154 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 155 | | /// <param name="includes">Navigations to include.</param> |
| | | 156 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 157 | | public static Task<TEntity?> GetAsync< TEntity >( |
| | | 158 | | this DbContext dbContext, int id, CancellationToken cancellationToken = default, params Expression<Func<TEnt |
| | | 159 | | ) |
| | | 160 | | where TEntity : class, IBaseEntity |
| | 0 | 161 | | => dbContext.GetAsync<TEntity, int>( id, cancellationToken, includes ); |
| | | 162 | | |
| | | 163 | | /// <summary><see cref="GetAsync{TEntity, TId}"/> for <see cref="Guid"/> keys.</summary> |
| | | 164 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 165 | | /// <param name="dbContext">The context.</param> |
| | | 166 | | /// <param name="id">The key.</param> |
| | | 167 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 168 | | /// <param name="includes">Navigations to include.</param> |
| | | 169 | | /// <returns>The entity, or <see langword="null"/>.</returns> |
| | | 170 | | public static Task<TEntity?> GetAsync< TEntity >( |
| | | 171 | | this DbContext dbContext, Guid id, CancellationToken cancellationToken = default, params Expression<Func<TEn |
| | | 172 | | ) |
| | | 173 | | where TEntity : class, IBaseGuidEntity |
| | 0 | 174 | | => dbContext.GetAsync<TEntity, Guid>( id, cancellationToken, includes ); |
| | | 175 | | |
| | | 176 | | /// <summary>Like <see cref="GetAsync{TEntity, TId}"/>, but the entity is tracked (changes are saved by the next <c> |
| | | 177 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 178 | | /// <typeparam name="TId">The key type.</typeparam> |
| | | 179 | | /// <param name="dbContext">The context.</param> |
| | | 180 | | /// <param name="id">The key.</param> |
| | | 181 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 182 | | /// <param name="includes">Navigations to include.</param> |
| | | 183 | | /// <returns>The tracked entity, or <see langword="null"/>.</returns> |
| | | 184 | | public static Task<TEntity?> GetTrackingAsync< TEntity, TId >( |
| | | 185 | | this DbContext dbContext, TId id, CancellationToken cancellationToken = default, params Expression<Func<TEnt |
| | | 186 | | ) |
| | | 187 | | where TEntity : class, IBaseEntity<TId> |
| | | 188 | | where TId : struct, IEquatable<TId> |
| | 0 | 189 | | => dbContext.Query( tracking: true, null, includes ).FirstOrDefaultAsync( t => t.Id.Equals( id ), cancellationTo |
| | | 190 | | |
| | | 191 | | /// <summary><see cref="GetTrackingAsync{TEntity, TId}"/> for <see cref="Guid"/> keys.</summary> |
| | | 192 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 193 | | /// <param name="dbContext">The context.</param> |
| | | 194 | | /// <param name="id">The key.</param> |
| | | 195 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 196 | | /// <param name="includes">Navigations to include.</param> |
| | | 197 | | /// <returns>The tracked entity, or <see langword="null"/>.</returns> |
| | | 198 | | public static Task<TEntity?> GetTrackingAsync< TEntity >( |
| | | 199 | | this DbContext dbContext, Guid id, CancellationToken cancellationToken = default, params Expression<Func<TEn |
| | | 200 | | ) |
| | | 201 | | where TEntity : class, IBaseGuidEntity |
| | 0 | 202 | | => dbContext.GetTrackingAsync<TEntity, Guid>( id, cancellationToken, includes ); |
| | | 203 | | |
| | | 204 | | /// <summary><see cref="GetTrackingAsync{TEntity, TId}"/> for <see cref="int"/> keys.</summary> |
| | | 205 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 206 | | /// <param name="dbContext">The context.</param> |
| | | 207 | | /// <param name="id">The key.</param> |
| | | 208 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 209 | | /// <param name="includes">Navigations to include.</param> |
| | | 210 | | /// <returns>The tracked entity, or <see langword="null"/>.</returns> |
| | | 211 | | public static Task<TEntity?> GetTrackingAsync< TEntity >( |
| | | 212 | | this DbContext dbContext, int id, CancellationToken cancellationToken = default, params Expression<Func<TEnt |
| | | 213 | | ) |
| | | 214 | | where TEntity : class, IBaseEntity |
| | 0 | 215 | | => dbContext.GetTrackingAsync<TEntity, int>( id, cancellationToken, includes ); |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Runs a stored procedure through the context's <see cref="IStoredProcedureExecutor"/> and maps the rows to |
| | | 219 | | /// <typeparamref name="TEntity"/>. Every value is sent as a parameter; <see langword="null"/> is sent as <c>NULL</c |
| | | 220 | | /// Server (<c>UseSqlServerInfrastructure()</c>) this is <c>EXEC [schema].[proc] @A = @A, …</c>. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <typeparam name="TEntity">A type in the model (usually keyless, <c>HasNoKey().ToView( null )</c>).</typeparam> |
| | | 223 | | /// <param name="dbContext">The context.</param> |
| | | 224 | | /// <param name="storedName">The procedure: <c>proc</c>, <c>schema.proc</c> or <c>[schema].[proc]</c>; a constant, n |
| | | 225 | | /// <param name="cmdType">The <c>@CmdType</c> value; <see langword="null"/> to leave it out.</param> |
| | | 226 | | /// <param name="parameters">The procedure parameters; keys are names with or without <c>@</c>.</param> |
| | | 227 | | /// <returns>The rows.</returns> |
| | | 228 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 229 | | /// <exception cref="ArgumentException"><paramref name="storedName"/> or a parameter name isn't a plain SQL identifi |
| | | 230 | | /// <exception cref="InvalidOperationException">No provider registered an <see cref="IStoredProcedureExecutor"/> for |
| | | 231 | | public static IReadOnlyList<TEntity> GetFromSql< TEntity >( this DbContext dbContext, string storedName, int? cmdTyp |
| | | 232 | | where TEntity : class |
| | | 233 | | { |
| | 0 | 234 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 235 | | |
| | 0 | 236 | | return dbContext.RequireStoredProcedureExecutor().GetFromSql<TEntity>( dbContext, storedName, cmdType, parameter |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary>Async version of <see cref="GetFromSql{TEntity}"/>.</summary> |
| | | 240 | | /// <typeparam name="TEntity">A type in the model (usually keyless, <c>HasNoKey().ToView( null )</c>).</typeparam> |
| | | 241 | | /// <param name="dbContext">The context.</param> |
| | | 242 | | /// <param name="storedName">The procedure: <c>proc</c>, <c>schema.proc</c> or <c>[schema].[proc]</c>; a constant, n |
| | | 243 | | /// <param name="cmdType">The <c>@CmdType</c> value; <see langword="null"/> to leave it out.</param> |
| | | 244 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 245 | | /// <param name="parameters">The procedure parameters; keys are names with or without <c>@</c>.</param> |
| | | 246 | | /// <returns>The rows.</returns> |
| | | 247 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 248 | | /// <exception cref="ArgumentException"><paramref name="storedName"/> or a parameter name isn't a plain SQL identifi |
| | | 249 | | /// <exception cref="InvalidOperationException">No provider registered an <see cref="IStoredProcedureExecutor"/> for |
| | | 250 | | public static Task<IReadOnlyList<TEntity>> GetFromSqlAsync< TEntity >( |
| | | 251 | | this DbContext dbContext, string storedName, int? cmdType = null, CancellationToken cancellationToken = defa |
| | | 252 | | ) |
| | | 253 | | where TEntity : class |
| | | 254 | | { |
| | 28 | 255 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 256 | | |
| | 28 | 257 | | return dbContext.RequireStoredProcedureExecutor().GetFromSqlAsync<TEntity>( dbContext, storedName, cmdType, para |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary>Returns a page of <paramref name="query"/>, read without tracking.</summary> |
| | | 261 | | /// <typeparam name="T">The row type.</typeparam> |
| | | 262 | | /// <param name="query">The query, already filtered and ordered.</param> |
| | | 263 | | /// <param name="page">1-based page number; values below 1 read page 1.</param> |
| | | 264 | | /// <param name="pageSize">Rows per page; 0 or less means 20, more than 100 means 100.</param> |
| | | 265 | | /// <returns>The page.</returns> |
| | | 266 | | public static PagedResult<T> GetPaged< T >( this IQueryable<T> query, int page, int pageSize ) |
| | | 267 | | where T : class |
| | | 268 | | { |
| | 0 | 269 | | ( page, pageSize ) = ClampPaging( page, pageSize ); |
| | 0 | 270 | | query = query.AsNoTracking(); |
| | | 271 | | |
| | | 272 | | |
| | 0 | 273 | | var count = query.Count(); |
| | 0 | 274 | | var items = count == 0 ? [] : query.Skip( ( page - 1 ) * pageSize ).Take( pageSize ).ToList(); |
| | | 275 | | |
| | | 276 | | |
| | 0 | 277 | | return ToPagedResult( items, page, pageSize, count ); |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary>Async version of <see cref="GetPaged{T}"/>.</summary> |
| | | 281 | | /// <typeparam name="T">The row type.</typeparam> |
| | | 282 | | /// <param name="query">The query, already filtered and ordered.</param> |
| | | 283 | | /// <param name="page">1-based page number; values below 1 read page 1.</param> |
| | | 284 | | /// <param name="pageSize">Rows per page; 0 or less means 20, more than 100 means 100.</param> |
| | | 285 | | /// <param name="cancellationToken">Token to cancel the query.</param> |
| | | 286 | | /// <returns>The page.</returns> |
| | | 287 | | public static async Task<PagedResult<T>> GetPagedAsync< T >( this IQueryable<T> query, int page, int pageSize, Cance |
| | | 288 | | where T : class |
| | | 289 | | { |
| | 0 | 290 | | ( page, pageSize ) = ClampPaging( page, pageSize ); |
| | 0 | 291 | | query = query.AsNoTracking(); |
| | | 292 | | |
| | | 293 | | |
| | 0 | 294 | | var count = await query.CountAsync( cancellationToken ).ConfigureAwait( false ); |
| | 0 | 295 | | var items = count == 0 ? [] : await query.Skip( ( page - 1 ) * pageSize ).Take( pageSize ).ToListAsync( cancella |
| | | 296 | | |
| | | 297 | | |
| | 0 | 298 | | return ToPagedResult( items, page, pageSize, count ); |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary>Returns the entity set, for composing queries.</summary> |
| | | 302 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 303 | | /// <param name="dbContext">The context.</param> |
| | | 304 | | /// <returns>The entity set.</returns> |
| | | 305 | | public static IQueryable<TEntity> GetQueryable< TEntity >( this DbContext dbContext ) |
| | | 306 | | where TEntity : class |
| | | 307 | | { |
| | 0 | 308 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 309 | | |
| | 0 | 310 | | return dbContext.Set<TEntity>(); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Runs a stored procedure as a command through the context's <see cref="IStoredProcedureExecutor"/> and returns it |
| | | 315 | | /// <c>@retVal</c> output (0 when the procedure leaves it <c>NULL</c>). Every value is sent as a parameter. On SQL S |
| | | 316 | | /// (<c>UseSqlServerInfrastructure()</c>) this is <c>EXEC [schema].[proc] @A = @A, …, @retVal = @retVal OUTPUT</c>. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <typeparam name="TEntity">Not used; kept so existing calls compile.</typeparam> |
| | | 319 | | /// <param name="dbContext">The context.</param> |
| | | 320 | | /// <param name="storedName">The procedure: <c>proc</c>, <c>schema.proc</c> or <c>[schema].[proc]</c>; a constant, n |
| | | 321 | | /// <param name="cmdType">The <c>@CmdType</c> value; <see langword="null"/> to leave it out.</param> |
| | | 322 | | /// <param name="parameters">The procedure parameters; keys are names with or without <c>@</c> (<c>retVal</c> is res |
| | | 323 | | /// <returns>The <c>@retVal</c> value.</returns> |
| | | 324 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 325 | | /// <exception cref="ArgumentException"><paramref name="storedName"/> or a parameter name isn't a plain SQL identifi |
| | | 326 | | /// <exception cref="InvalidOperationException">No provider registered an <see cref="IStoredProcedureExecutor"/> for |
| | | 327 | | public static int OperateFromSql< TEntity >( this DbContext dbContext, string storedName, int? cmdType = null, param |
| | | 328 | | where TEntity : class |
| | | 329 | | { |
| | 0 | 330 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 331 | | |
| | 0 | 332 | | return dbContext.RequireStoredProcedureExecutor().OperateFromSql( dbContext, storedName, cmdType, parameters ); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary>Async version of <see cref="OperateFromSql{TEntity}"/>.</summary> |
| | | 336 | | /// <typeparam name="TEntity">Not used; kept so existing calls compile.</typeparam> |
| | | 337 | | /// <param name="dbContext">The context.</param> |
| | | 338 | | /// <param name="storedName">The procedure: <c>proc</c>, <c>schema.proc</c> or <c>[schema].[proc]</c>; a constant, n |
| | | 339 | | /// <param name="cmdType">The <c>@CmdType</c> value; <see langword="null"/> to leave it out.</param> |
| | | 340 | | /// <param name="cancellationToken">Token to cancel the call.</param> |
| | | 341 | | /// <param name="parameters">The procedure parameters; keys are names with or without <c>@</c> (<c>retVal</c> is res |
| | | 342 | | /// <returns>The <c>@retVal</c> value.</returns> |
| | | 343 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | | 344 | | /// <exception cref="ArgumentException"><paramref name="storedName"/> or a parameter name isn't a plain SQL identifi |
| | | 345 | | /// <exception cref="InvalidOperationException">No provider registered an <see cref="IStoredProcedureExecutor"/> for |
| | | 346 | | public static Task<int> OperateFromSqlAsync< TEntity >( |
| | | 347 | | this DbContext dbContext, string storedName, int? cmdType = null, CancellationToken cancellationToken = defa |
| | | 348 | | ) |
| | | 349 | | where TEntity : class |
| | | 350 | | { |
| | 12 | 351 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 352 | | |
| | 12 | 353 | | return dbContext.RequireStoredProcedureExecutor().OperateFromSqlAsync( dbContext, storedName, cmdType, parameter |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | |
| | | 357 | | private static (int Page, int PageSize) ClampPaging( int page, int pageSize ) |
| | 0 | 358 | | => ( Math.Max( page, 1 ), pageSize switch |
| | 0 | 359 | | { |
| | 0 | 360 | | <= 0 => 20, |
| | 0 | 361 | | > 100 => 100, |
| | 0 | 362 | | _ => pageSize |
| | 0 | 363 | | } ); |
| | | 364 | | |
| | | 365 | | private static PagedResult<T> ToPagedResult< T >( List<T> items, int page, int pageSize, int count ) |
| | | 366 | | where T : class |
| | 0 | 367 | | => new() |
| | 0 | 368 | | { |
| | 0 | 369 | | PageNumber = page, |
| | 0 | 370 | | PageSize = pageSize, |
| | 0 | 371 | | TotalCount = count, |
| | 0 | 372 | | TotalPages = items.Count == 0 ? 0 : (int) Math.Ceiling( (double) count / pageSize ), |
| | 0 | 373 | | Items = items |
| | 0 | 374 | | }; |
| | | 375 | | } |