DbExtention.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Microsoft.Data.SqlClient;
  2. using Microsoft.EntityFrameworkCore;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace EVCB_OCPP.WSServer.Helper
  10. {
  11. public static class DbExtention
  12. {
  13. public static async Task<(bool, int)> SaveChangesUntilSuccessAsync(this DbContext context)
  14. {
  15. bool result = false;
  16. int rows = 0;
  17. do
  18. {
  19. (result, rows) = await context.TrySaveChangesAsync();
  20. }
  21. while (!result);
  22. return (result, rows);
  23. }
  24. public static async Task<(bool, int)> TrySaveChangesAsync(this DbContext context)
  25. {
  26. int affectedRows = 0;
  27. try
  28. {
  29. affectedRows = await context.SaveChangesAsync();
  30. return (true, affectedRows);
  31. }
  32. catch (Exception e)
  33. {
  34. return (false, affectedRows);
  35. }
  36. }
  37. public static void AddInsertMeterValueRecordSqlParameters(this List<SqlParameter> sqlParameters,
  38. string chargeBoxId, byte connectorId, decimal value, DateTime createdOn
  39. , int contextId, int formatId, int measurandId, int phaseId
  40. , int locationId, int unitId, int transactionId)
  41. {
  42. List<SqlParameter> parameter = new List<SqlParameter>
  43. {
  44. new SqlParameter("ChargeBoxId", SqlDbType.NVarChar, 50){ Value = chargeBoxId },
  45. new SqlParameter("ConnectorId", SqlDbType.TinyInt) { Value = connectorId },
  46. new SqlParameter("Value", SqlDbType.Decimal) { Value = value, Precision = 18, Scale = 8 },
  47. new SqlParameter("CreatedOn", SqlDbType.DateTime) { Value = createdOn },
  48. new SqlParameter("ContextId", SqlDbType.Int) { Value = contextId },
  49. new SqlParameter("FormatId", SqlDbType.Int) { Value = formatId },
  50. new SqlParameter("MeasurandId", SqlDbType.Int) { Value = measurandId },
  51. new SqlParameter("PhaseId", SqlDbType.Int) { Value = phaseId },
  52. new SqlParameter("LocationId", SqlDbType.Int) { Value = locationId },
  53. new SqlParameter("UnitId", SqlDbType.Int) { Value = unitId },
  54. new SqlParameter("TransactionId", SqlDbType.Int) { Value = transactionId },
  55. };
  56. sqlParameters.AddRange(parameter);
  57. }
  58. public static void AddInsertConnectorStatusRecordSqlParameters(this List<SqlParameter> sqlParameters,
  59. string chargeBoxId, byte connectorId, int status, DateTime createdOn
  60. , string errorInfo, string vendorId, string vendorErrorCode, int chargePointErrorCodeId)
  61. {
  62. List<SqlParameter> parameter = new List<SqlParameter>
  63. {
  64. new SqlParameter("ChargeBoxId", SqlDbType.NVarChar, 50){ Value = chargeBoxId },
  65. new SqlParameter("ConnectorId", SqlDbType.TinyInt) { Value = connectorId },
  66. new SqlParameter("Status", SqlDbType.Int){ Value = status },
  67. new SqlParameter("CreatedOn", SqlDbType.DateTime) { Value = createdOn },
  68. new SqlParameter("ChargePointErrorCodeId", SqlDbType.Int) { Value = chargePointErrorCodeId },
  69. new SqlParameter("ErrorInfo",SqlDbType.NVarChar, 50){ Value = errorInfo is null ? DBNull.Value : errorInfo },
  70. new SqlParameter("VendorId", SqlDbType.NVarChar, 255){ Value = vendorId is null ? DBNull.Value : vendorId },
  71. new SqlParameter("VendorErrorCode", SqlDbType.NVarChar, 100) {Value =vendorErrorCode is null ? DBNull.Value : vendorErrorCode},
  72. };
  73. sqlParameters.AddRange(parameter);
  74. }
  75. }
  76. }