DbContextService.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using GreenTree.Nachtragsmanagement.Core;
  8. using GreenTree.Nachtragsmanagement.Data;
  9. namespace GreenTree.Nachtragsmanagement.Services.DbContext
  10. {
  11. public class DbContextService : IDbContextService
  12. {
  13. #region Fields
  14. /// <summary>
  15. /// Current database context
  16. /// </summary>
  17. private readonly IDbContext _dbContext;
  18. #endregion
  19. #region Ctor
  20. /// <summary>
  21. /// Initializes a new instance of the DbContextService class
  22. /// </summary>
  23. public DbContextService(IDbContext iDbContext)
  24. {
  25. _dbContext = iDbContext;
  26. }
  27. #endregion
  28. #region Services
  29. /// <summary>
  30. /// Checks if a DbSet exists in the curent DbContext
  31. /// </summary>
  32. /// <typeparam name="TEntity">The entity type of the DbSet.</typeparam>
  33. public bool DbSetExists<TEntity>() where TEntity : BaseEntity
  34. {
  35. var dbSet = _dbContext.Get<TEntity>();
  36. return dbSet != null;
  37. }
  38. /// <summary>
  39. /// Get the DbSet of the corresponding Entity type
  40. /// </summary>
  41. /// <typeparam name="TEntity">The entity type of the DbSet.</typeparam>
  42. public IDbSet<TEntity> GetDbSet<TEntity>() where TEntity : BaseEntity
  43. {
  44. var dbSet = _dbContext.Get<TEntity>();
  45. return dbSet;
  46. }
  47. /// <summary>
  48. /// Gets the current DbContext
  49. /// </summary>
  50. public AppendixObjectContext GetDbContext()
  51. {
  52. return (AppendixObjectContext)_dbContext;
  53. }
  54. #endregion
  55. }
  56. }