using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GreenTree.Nachtragsmanagement.Core;
namespace GreenTree.Nachtragsmanagement.Data
{
public class AppendixObjectContext : DbContext, IDbContext
{
#region Ctor
///
/// Initializes a new instance of the AppendixObjectContext-class
///
///
public AppendixObjectContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
//((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
}
#endregion
#region Utilities
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
///
/// Attach an entity to the context or return an already attached entity (if it was already attached)
///
/// TEntity
/// Entity
/// Attached entity
protected virtual TEntity AttachEntityToContext(TEntity entity) where TEntity : BaseEntity, new()
{
//little hack here until Entity Framework really supports stored procedures
//otherwise, navigation properties of loaded entities are not loaded until an entity is attached to the context
var alreadyAttached = Set().Local.FirstOrDefault(x => x.Id == entity.Id);
if (alreadyAttached == null)
{
//attach new entity
Set().Attach(entity);
return entity;
}
//entity is already loaded
return alreadyAttached;
}
#endregion
#region Methods
///
/// Create database script
///
/// SQL to generate database
public string CreateDatabaseScript()
{
return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
}
///
/// Get DbSet
///
/// Entity type
/// DbSet
public new IDbSet Set() where TEntity : BaseEntity
{
return base.Set();
}
///
/// 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.
///
/// The type of object returned by the query.
/// The SQL query string.
/// The parameters to apply to the SQL query string.
/// Result
public IEnumerable SqlQuery(string sql, params object[] parameters)
{
return this.Database.SqlQuery(sql, parameters);
}
///
/// Executes the given DDL/DML command against the database.
///
/// The command string
/// false - the transaction creation is not ensured; true - the transaction creation is ensured.
/// Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used
/// The parameters to apply to the command string.
/// The result returned by the database after executing the command.
public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
{
int? previousTimeout = null;
if (timeout.HasValue)
{
//store previous timeout
previousTimeout = ((IObjectContextAdapter)this).ObjectContext.CommandTimeout;
((IObjectContextAdapter)this).ObjectContext.CommandTimeout = timeout;
}
var transactionalBehavior = doNotEnsureTransaction
? TransactionalBehavior.DoNotEnsureTransaction
: TransactionalBehavior.EnsureTransaction;
var result = this.Database.ExecuteSqlCommand(transactionalBehavior, sql, parameters);
if (timeout.HasValue)
{
//Set previous timeout back
((IObjectContextAdapter)this).ObjectContext.CommandTimeout = previousTimeout;
}
//return result
return result;
}
#endregion
}
}