DbSetExtensions.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Microsoft.EntityFrameworkCore;
  9. using Microsoft.EntityFrameworkCore.Infrastructure;
  10. using Microsoft.EntityFrameworkCore.Internal;
  11. using Microsoft.EntityFrameworkCore.Storage;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using EasyDevCore.Common;
  14. using System.Data;
  15. using System.Runtime.CompilerServices;
  16. using System.Dynamic;
  17. using System.Collections;
  18. using System.Reflection;
  19. using Microsoft.EntityFrameworkCore.Metadata;
  20. using Microsoft.EntityFrameworkCore.Query;
  21. using System.Linq.Expressions;
  22. using EasyDevCore.Database.EntityFrameworkCore;
  23. namespace EasyDevCore.Database.EntityFrameworkCore
  24. {
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public static class DbSetExtensions
  29. {
  30. /// <summary>
  31. /// Gets the context.
  32. /// </summary>
  33. /// <typeparam name="TEntity">The type of the entity.</typeparam>
  34. /// <param name="dbSet">The database set.</param>
  35. /// <returns></returns>
  36. public static DbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
  37. {
  38. object internalSet = dbSet
  39. .GetType()
  40. .GetField("_internalSet", BindingFlags.NonPublic | BindingFlags.Instance)
  41. .GetValue(dbSet);
  42. object internalContext = internalSet
  43. .GetType()
  44. .BaseType
  45. .GetField("_internalContext", BindingFlags.NonPublic | BindingFlags.Instance)
  46. .GetValue(internalSet);
  47. return (DbContext)internalContext
  48. .GetType()
  49. .GetProperty("Owner", BindingFlags.Instance | BindingFlags.Public)
  50. .GetValue(internalContext, null);
  51. }
  52. /// <summary>
  53. /// Saves the change.
  54. /// </summary>
  55. /// <typeparam name="TEntity">The type of the entity.</typeparam>
  56. /// <param name="dbSet">The database set.</param>
  57. /// <returns></returns>
  58. public static int SaveChange<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
  59. {
  60. var context = (EasyDbContext)dbSet.GetContext();
  61. return context.SaveChangeEntity(dbSet.GetType());
  62. }
  63. /// <summary>
  64. /// Saves the change asynchronous.
  65. /// </summary>
  66. /// <typeparam name="TEntity">The type of the entity.</typeparam>
  67. /// <param name="dbSet">The database set.</param>
  68. /// <returns></returns>
  69. public static async Task<int> SaveChangeAsync<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class
  70. {
  71. var context = (EasyDbContext)dbSet.GetContext();
  72. return await context.SaveChangeEntityAsync(dbSet.GetType()).ConfigureAwait(false);
  73. }
  74. }
  75. }