diff --git a/DemoApplication/DatabaseContext/UserManagementDbContext.cs b/DemoApplication/DatabaseContext/UserManagementDbContext.cs index ad64e86..0b1e540 100644 --- a/DemoApplication/DatabaseContext/UserManagementDbContext.cs +++ b/DemoApplication/DatabaseContext/UserManagementDbContext.cs @@ -1,10 +1,11 @@ using System.Data.Entity; using System.Reflection; +using Mehdime.Entity; using Numero3.EntityFramework.Demo.DomainModel; namespace Numero3.EntityFramework.Demo.DatabaseContext { - public class UserManagementDbContext : DbContext + public class UserManagementDbContext : DbContext, IDbContext { // Map our 'User' model by convention public DbSet Users { get; set; } diff --git a/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs b/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs index d182fc9..f16bb22 100644 --- a/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs +++ b/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs @@ -11,7 +11,7 @@ namespace Mehdime.Entity { public class AmbientDbContextLocator : IAmbientDbContextLocator { - public TDbContext Get() where TDbContext : DbContext + public TDbContext Get() where TDbContext : class, IDbContext { var ambientDbContextScope = DbContextScope.GetAmbientScope(); return ambientDbContextScope == null ? null : ambientDbContextScope.DbContexts.Get(); diff --git a/Mehdime.Entity/Implementations/DbContextCollection.cs b/Mehdime.Entity/Implementations/DbContextCollection.cs index 578790e..24b6909 100644 --- a/Mehdime.Entity/Implementations/DbContextCollection.cs +++ b/Mehdime.Entity/Implementations/DbContextCollection.cs @@ -29,30 +29,30 @@ namespace Mehdime.Entity /// public class DbContextCollection : IDbContextCollection { - private Dictionary _initializedDbContexts; - private Dictionary _transactions; + private Dictionary _initializedDbContexts; + private Dictionary _transactions; private IsolationLevel? _isolationLevel; private readonly IDbContextFactory _dbContextFactory; private bool _disposed; private bool _completed; private bool _readOnly; - internal Dictionary InitializedDbContexts { get { return _initializedDbContexts; } } + internal Dictionary InitializedDbContexts { get { return _initializedDbContexts; } } public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel = null, IDbContextFactory dbContextFactory = null) { _disposed = false; _completed = false; - _initializedDbContexts = new Dictionary(); - _transactions = new Dictionary(); + _initializedDbContexts = new Dictionary(); + _transactions = new Dictionary(); _readOnly = readOnly; _isolationLevel = isolationLevel; _dbContextFactory = dbContextFactory; } - public TDbContext Get() where TDbContext : DbContext + public TDbContext Get() where TDbContext : class, IDbContext { if (_disposed) throw new ObjectDisposedException("DbContextCollection"); diff --git a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs index b91cc1f..3d52d05 100644 --- a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs +++ b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs @@ -5,21 +5,21 @@ * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ -using System.Data.Entity; + +using System; namespace Mehdime.Entity { /// - /// Convenience methods to retrieve ambient DbContext instances. + /// Convenience methods to retrieve ambient DbContext instances. /// public interface IAmbientDbContextLocator { /// - /// If called within the scope of a DbContextScope, gets or creates - /// the ambient DbContext instance for the provided DbContext type. - /// - /// Otherwise returns null. + /// If called within the scope of a DbContextScope, gets or creates + /// the ambient DbContext instance for the provided DbContext type. + /// Otherwise returns null. /// - TDbContext Get() where TDbContext : DbContext; + TDbContext Get() where TDbContext : class, IDbContext; } -} +} \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContext.cs b/Mehdime.Entity/Interfaces/IDbContext.cs new file mode 100644 index 0000000..662710d --- /dev/null +++ b/Mehdime.Entity/Interfaces/IDbContext.cs @@ -0,0 +1,19 @@ +using System; +using System.Data.Entity; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Mehdime.Entity +{ + public interface IDbContext : IDisposable + { + DbContextConfiguration Configuration { get; } + Database Database { get; } + + int SaveChanges(); + + Task SaveChangesAsync(CancellationToken cancelToken); + } +} \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContextCollection.cs b/Mehdime.Entity/Interfaces/IDbContextCollection.cs index 5476c6c..adbc231 100644 --- a/Mehdime.Entity/Interfaces/IDbContextCollection.cs +++ b/Mehdime.Entity/Interfaces/IDbContextCollection.cs @@ -18,6 +18,6 @@ public interface IDbContextCollection : IDisposable /// /// Get or create a DbContext instance of the specified type. /// - TDbContext Get() where TDbContext : DbContext; + TDbContext Get() where TDbContext : class, IDbContext; } } \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContextFactory.cs b/Mehdime.Entity/Interfaces/IDbContextFactory.cs index f8291cd..74c6e8f 100644 --- a/Mehdime.Entity/Interfaces/IDbContextFactory.cs +++ b/Mehdime.Entity/Interfaces/IDbContextFactory.cs @@ -33,6 +33,6 @@ namespace Mehdime.Entity /// public interface IDbContextFactory { - TDbContext CreateDbContext() where TDbContext : DbContext; + TDbContext CreateDbContext() where TDbContext : class, IDbContext; } } diff --git a/Mehdime.Entity/Mehdime.Entity.csproj b/Mehdime.Entity/Mehdime.Entity.csproj index 6064367..9c52974 100644 --- a/Mehdime.Entity/Mehdime.Entity.csproj +++ b/Mehdime.Entity/Mehdime.Entity.csproj @@ -56,6 +56,7 @@ +