AppendixObjectContext.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using GreenTree.Nachtragsmanagement.Core;
  11. namespace GreenTree.Nachtragsmanagement.Data
  12. {
  13. public class AppendixObjectContext : DbContext, IDbContext
  14. {
  15. #region Ctor
  16. /// <summary>
  17. /// Initializes a new instance of the AppendixObjectContext-class
  18. /// </summary>
  19. /// <param name="nameOrConnectionString"></param>
  20. public AppendixObjectContext(string nameOrConnectionString)
  21. : base(nameOrConnectionString)
  22. {
  23. //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
  24. }
  25. #endregion
  26. #region Utilities
  27. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  28. {
  29. base.OnModelCreating(modelBuilder);
  30. }
  31. /// <summary>
  32. /// Attach an entity to the context or return an already attached entity (if it was already attached)
  33. /// </summary>
  34. /// <typeparam name="TEntity">TEntity</typeparam>
  35. /// <param name="entity">Entity</param>
  36. /// <returns>Attached entity</returns>
  37. protected virtual TEntity AttachEntityToContext<TEntity>(TEntity entity) where TEntity : BaseEntity, new()
  38. {
  39. //little hack here until Entity Framework really supports stored procedures
  40. //otherwise, navigation properties of loaded entities are not loaded until an entity is attached to the context
  41. var alreadyAttached = Set<TEntity>().Local.FirstOrDefault(x => x.Id == entity.Id);
  42. if (alreadyAttached == null)
  43. {
  44. //attach new entity
  45. Set<TEntity>().Attach(entity);
  46. return entity;
  47. }
  48. //entity is already loaded
  49. return alreadyAttached;
  50. }
  51. #endregion
  52. #region Methods
  53. /// <summary>
  54. /// Create database script
  55. /// </summary>
  56. /// <returns>SQL to generate database</returns>
  57. public string CreateDatabaseScript()
  58. {
  59. return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
  60. }
  61. /// <summary>
  62. /// Get DbSet
  63. /// </summary>
  64. /// <typeparam name="TEntity">Entity type</typeparam>
  65. /// <returns>DbSet</returns>
  66. public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
  67. {
  68. return base.Set<TEntity>();
  69. }
  70. /// <summary>
  71. /// Creates a raw SQL query that will return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
  72. /// </summary>
  73. /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
  74. /// <param name="sql">The SQL query string.</param>
  75. /// <param name="parameters">The parameters to apply to the SQL query string.</param>
  76. /// <returns>Result</returns>
  77. public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
  78. {
  79. return this.Database.SqlQuery<TElement>(sql, parameters);
  80. }
  81. /// <summary>
  82. /// Executes the given DDL/DML command against the database.
  83. /// </summary>
  84. /// <param name="sql">The command string</param>
  85. /// <param name="doNotEnsureTransaction">false - the transaction creation is not ensured; true - the transaction creation is ensured.</param>
  86. /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
  87. /// <param name="parameters">The parameters to apply to the command string.</param>
  88. /// <returns>The result returned by the database after executing the command.</returns>
  89. public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
  90. {
  91. int? previousTimeout = null;
  92. if (timeout.HasValue)
  93. {
  94. //store previous timeout
  95. previousTimeout = ((IObjectContextAdapter)this).ObjectContext.CommandTimeout;
  96. ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = timeout;
  97. }
  98. var transactionalBehavior = doNotEnsureTransaction
  99. ? TransactionalBehavior.DoNotEnsureTransaction
  100. : TransactionalBehavior.EnsureTransaction;
  101. var result = this.Database.ExecuteSqlCommand(transactionalBehavior, sql, parameters);
  102. if (timeout.HasValue)
  103. {
  104. //Set previous timeout back
  105. ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = previousTimeout;
  106. }
  107. //return result
  108. return result;
  109. }
  110. #endregion
  111. }
  112. }