UnitOfWork.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore.Storage;
  3. using Ropin.Inspection.Model.Entities;
  4. using Ropin.Inspection.Repository.Interface;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.Common;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Ropin.Inspection.Repository
  13. {
  14. public class UnitOfWork : IUnitOfWork
  15. {
  16. private InspectionDbContext _dbContext;
  17. private IDbContextTransaction _dbTransaction;
  18. public UnitOfWork(InspectionDbContext dbContext)
  19. {
  20. _dbContext = dbContext;
  21. }
  22. public void BeginTransaction()
  23. {
  24. _dbTransaction = _dbContext.Database.BeginTransaction();
  25. }
  26. public async Task<int> ExecuteSqlCommandAsync(string sql, DbParameter[] sqlParams )
  27. {
  28. int result = -1;
  29. var db = _dbContext.Database;
  30. var connection = db.GetDbConnection();
  31. var cmd = connection.CreateCommand();
  32. if (connection.State == ConnectionState.Closed)
  33. connection.Open();
  34. //using (var transaction = connection.BeginTransaction())
  35. //{
  36. using (var command = connection.CreateCommand())
  37. {
  38. cmd.CommandText = sql;
  39. cmd.CommandType = CommandType.Text;
  40. if (sqlParams != null)
  41. {
  42. cmd.Parameters.AddRange(sqlParams);
  43. }
  44. // *** ADD THIS LINE ***
  45. //command.Transaction = transaction;
  46. // otherwise, this will throw System.InvalidOperationException: The transaction associated with this command is not the connection's active transaction.
  47. result = command.ExecuteNonQuery();
  48. }
  49. //}
  50. connection.Close();
  51. return await Task.FromResult(result) ;
  52. //int i = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(_dbContext.Database, sql, sqlParams);
  53. //return await Task.FromResult(i);
  54. //return await _dbContext.Database.ExecuteSqlRawAsync(sql, parameters);//.ExecuteSqlCommandAsync(sql, parameters);
  55. }
  56. public async Task<bool> RegisterNew<TEntity>(TEntity entity)
  57. where TEntity : class
  58. {
  59. _dbContext.Set<TEntity>().Add(entity);
  60. if (_dbTransaction != null)
  61. return await _dbContext.SaveChangesAsync() > 0;
  62. return true;
  63. }
  64. public async Task<bool> RegisterRangeNew<TEntity>(IEnumerable<TEntity> entities)
  65. where TEntity : class
  66. {
  67. _dbContext.Set<TEntity>().AddRange(entities);
  68. if (_dbTransaction != null)
  69. return await _dbContext.SaveChangesAsync()>0;
  70. return true;
  71. }
  72. public async Task<bool> RegisterDirty<TEntity>(TEntity entity)
  73. where TEntity : class
  74. {
  75. _dbContext.Entry<TEntity>(entity).State = EntityState.Modified;
  76. if (_dbTransaction != null)
  77. return await _dbContext.SaveChangesAsync() > 0;
  78. return true;
  79. }
  80. public async Task<bool> RegisterClean<TEntity>(TEntity entity)
  81. where TEntity : class
  82. {
  83. _dbContext.Entry<TEntity>(entity).State = EntityState.Unchanged;
  84. if (_dbTransaction != null)
  85. return await _dbContext.SaveChangesAsync() > 0;
  86. return true;
  87. }
  88. public async Task<bool> RegisterDeleted<TEntity>(TEntity entity)
  89. where TEntity : class
  90. {
  91. _dbContext.Set<TEntity>().Remove(entity);
  92. if (_dbTransaction != null)
  93. return await _dbContext.SaveChangesAsync() > 0;
  94. return true;
  95. }
  96. public async Task<bool> CommitAsync()
  97. {
  98. if (_dbTransaction == null)
  99. return await _dbContext.SaveChangesAsync() > 0;
  100. else
  101. _dbTransaction.Commit();
  102. return true;
  103. }
  104. public void Rollback()
  105. {
  106. if (_dbTransaction != null)
  107. _dbTransaction.Rollback();
  108. }
  109. }
  110. }