123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using Microsoft.EntityFrameworkCore;
- using System.Data;
- namespace EasyDevCore.Database
- {
- /// <summary>
- ///
- /// </summary>
- public static class DbContextExtensions
- {
- /// <summary>
- /// Changes the database.
- /// </summary>
- /// <param name="dbContext">The database context.</param>
- /// <param name="databaseName">Name of the database.</param>
- public static void ChangeDatabase(this DbContext dbContext, string databaseName)
- {
- dbContext.Database.OpenConnection();
- dbContext.Database.GetDbConnection().ChangeDatabase(databaseName);
- }
- /// <summary>
- /// Determines whether [is SQL server].
- /// </summary>
- /// <param name="dbContext">The database context.</param>
- /// <returns>
- /// <c>true</c> if [is SQL server] [the specified database context]; otherwise, <c>false</c>.
- /// </returns>
- public static bool IsSQLServer(this DbContext dbContext)
- {
- return dbContext.Database.ProviderName.EndsWith("SQLServer", StringComparison.InvariantCultureIgnoreCase);
- }
- /// <summary>
- /// Determines whether [is my SQL].
- /// </summary>
- /// <param name="dbContext">The database context.</param>
- /// <returns>
- /// <c>true</c> if [is my SQL] [the specified database context]; otherwise, <c>false</c>.
- /// </returns>
- public static bool IsMySQL(this DbContext dbContext)
- {
- return dbContext.Database.ProviderName.EndsWith("MySqlClient", StringComparison.InvariantCultureIgnoreCase);
- }
- /// <summary>
- /// Determines whether this instance is Posgres SQL.
- /// </summary>
- /// <param name="dbContext">The database context.</param>
- /// <returns>
- /// <c>true</c> if the specified database context is NPGSQL; otherwise, <c>false</c>.
- /// </returns>
- /// <exception cref="NotImplementedException">IsNpgsql</exception>
- public static bool IsNpgsql(this DbContext dbContext)
- {
- return false;
- }
- /// <summary>
- /// Determines whether this instance is Oracle.
- /// </summary>
- /// <param name="dbContext">The database context.</param>
- /// <returns>
- /// <c>true</c> if the specified database context is oracle; otherwise, <c>false</c>.
- /// </returns>
- /// <exception cref="NotImplementedException">IsOracle</exception>
- public static bool IsOracle(this DbContext dbContext)
- {
- return false;
- }
- /// <summary>
- /// Saves the change entity.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <param name="type">The type.</param>
- /// <param name="acceptAllChangesOnSuccess">if set to <c>true</c> [accept all changes on success].</param>
- /// <returns></returns>
- internal static int SaveChangeEntity(this DbContext context, Type type, bool acceptAllChangesOnSuccess = true)
- {
- var original = context.ChangeTracker.Entries()
- .Where(x => !type.IsAssignableFrom(x.Entity.GetType()) && x.State != EntityState.Unchanged)
- .GroupBy(x => x.State)
- .ToList();
- int rows = 0;
- try
- {
- foreach (var entry in context.ChangeTracker.Entries().Where(x => !type.IsAssignableFrom(x.Entity.GetType())))
- {
- entry.State = EntityState.Unchanged;
- }
- rows = context.SaveChanges(acceptAllChangesOnSuccess);
- }
- finally
- {
- foreach (var state in original)
- {
- foreach (var entry in state)
- {
- entry.State = state.Key;
- }
- }
- }
- return rows;
- }
- /// <summary>
- /// Saves the change entity asynchronous.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <param name="type">The type.</param>
- /// <param name="acceptAllChangesOnSuccess">if set to <c>true</c> [accept all changes on success].</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns></returns>
- internal static async Task<int> SaveChangeEntityAsync(this DbContext context, Type type, bool acceptAllChangesOnSuccess = true, CancellationToken cancellationToken = default)
- {
- var original = context.ChangeTracker.Entries()
- .Where(x => !type.IsAssignableFrom(x.Entity.GetType()) && x.State != EntityState.Unchanged)
- .GroupBy(x => x.State)
- .ToList();
- int rows = 0;
- try
- {
- foreach (var entry in context.ChangeTracker.Entries().Where(x => !type.IsAssignableFrom(x.Entity.GetType())))
- {
- entry.State = EntityState.Unchanged;
- }
- rows = await context.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);
- }
- finally
- {
- foreach (var state in original)
- {
- foreach (var entry in state)
- {
- entry.State = state.Key;
- }
- }
- }
- return rows;
- }
- /// <summary>
- /// Saves the changes.
- /// </summary>
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
- /// <param name="dbContext">The database context.</param>
- /// <param name="acceptAllChangesOnSuccess">if set to <c>true</c> [accept all changes on success].</param>
- /// <param name="entities">The entities.</param>
- /// <returns></returns>
- public static int SaveChanges<TEntity>(this DbContext dbContext, bool acceptAllChangesOnSuccess, params TEntity[] entities) where TEntity : IQueryable
- {
- int rows = 0;
- foreach (var item in entities)
- {
- rows += dbContext.SaveChangeEntity(item.GetType(), acceptAllChangesOnSuccess);
- }
- return rows;
- }
- /// <summary>
- /// Saves the changes.
- /// </summary>
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
- /// <param name="dbContext">The database context.</param>
- /// <param name="entities">The entities.</param>
- /// <returns></returns>
- public static int SaveChanges<TEntity>(this EasyDbContext dbContext, params TEntity[] entities) where TEntity : IQueryable
- {
- return dbContext.SaveChanges(true, entities);
- }
- /// <summary>
- /// Saves the changes asynchronous.
- /// </summary>
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
- /// <param name="dbContext">The database context.</param>
- /// <param name="acceptAllChangesOnSuccess">if set to <c>true</c> [accept all changes on success].</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <param name="entities">The entities.</param>
- /// <returns></returns>
- public static async Task<int> SaveChangesAsync<TEntity>(this DbContext dbContext, bool acceptAllChangesOnSuccess, CancellationToken cancellationToken, params TEntity[] entities) where TEntity : IQueryable
- {
- int rows = 0;
- foreach (var item in entities)
- {
- rows += await dbContext.SaveChangeEntityAsync(item.GetType(), acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false);
- }
- return rows;
- }
- /// <summary>
- /// Saves the changes asynchronous.
- /// </summary>
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
- /// <param name="dbContext">The database context.</param>
- /// <param name="acceptAllChangesOnSuccess">if set to <c>true</c> [accept all changes on success].</param>
- /// <param name="entities">The entities.</param>
- /// <returns></returns>
- public static async Task<int> SaveChangesAsync<TEntity>(this DbContext dbContext, bool acceptAllChangesOnSuccess, params TEntity[] entities) where TEntity : IQueryable
- {
- return await dbContext.SaveChangesAsync(acceptAllChangesOnSuccess, default, entities).ConfigureAwait(false);
- }
- /// <summary>
- /// Saves the changes asynchronous.
- /// </summary>
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
- /// <param name="dbContext">The database context.</param>
- /// <param name="entities">The entities.</param>
- /// <returns></returns>
- public static async Task<int> SaveChangesAsync<TEntity>(this DbContext dbContext, params TEntity[] entities) where TEntity : IQueryable
- {
- return await dbContext.SaveChangesAsync(true, default, entities).ConfigureAwait(false);
- }
- }
- }
|