Why does DbContext not implement the IDbContext interface?

Why is there no IDbContext interface in the Entity Framework? Wouldn’t it be easier to test things if there was an interface with methods such as SaveChanges () etc., from which you could get your database user context interface?

 public interface ICustomDbContext : IDbContext { // add entity set properties to existing set of methods in IDbContext IDbSet<SomeEntity> SomeEntities { get; } } 
+45
unit-testing testing moq entity-framework mocking
04 Sep
source share
4 answers

I see this IDbContext :

See this link. And then you will create a new partial class for the Entities Context With That interface.

 public partial class YourModelEntities : DbContext, IDbContext 

Editorial: I edited this post, This Works for me. My context

 namespace dao { public interface ContextI : IDisposable { DbSet<TEntity> Set<TEntity>() where TEntity : class; DbSet Set(Type entityType); int SaveChanges(); IEnumerable<DbEntityValidationResult> GetValidationErrors(); DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity:class; DbEntityEntry Entry(object entity); string ConnectionString { get; set; } bool AutoDetectChangedEnabled { get; set; } void ExecuteSqlCommand(string p, params object[] o); void ExecuteSqlCommand(string p); } } 

YourModelEntities is your automatically generated partial class, and you need to create a new partial class with the same name and then add a new context interface, for this example ContextI

NOTE. The interface did not implement all the methods, because the methods are implemented in your automatic code generation.

 namespace dao { public partial class YourModelEntities :DbContext, ContextI { public string ConnectionString { get { return this.Database.Connection.ConnectionString; } set { this.Database.Connection.ConnectionString = value; } } bool AutoDetectChangedEnabled { get { return true; } set { throw new NotImplementedException(); } } public void ExecuteSqlCommand(string p,params object[] os) { this.Database.ExecuteSqlCommand(p, os); } public void ExecuteSqlCommand(string p) { this.Database.ExecuteSqlCommand(p); } bool ContextI.AutoDetectChangedEnabled { get { return this.Configuration.AutoDetectChangesEnabled; } set { this.Configuration.AutoDetectChangesEnabled = value; } } } } 
+11
Nov 26 '13 at 21:49
source share
— -

I also thought about this, I assume that you are going to use it to ridicule DbContext . I find no reason for this, except that you will need to manually implement your own DbSet manually for your mocking class (so in any case you will have to rewrite your own interface).

0
Nov 22 '17 at 1:52
source share

Just create a DbContext layout that extends your DbContext product by overriding methods that make testing harder. Thus, any changes to the production DbContext are automatically reflected in the tests, with the exception of overridden methods. For any other persistence classes that take DbContext, just extend them as well as passing in an extended DbContext layout.

 namespace Test.Mocks { public sealed class MockDatabaseContext : MainProject.Persistence.Database.DatabaseContext { public MockDatabaseContext(ConfigurationWrapper config) : base(config) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var dbPath = "test.db"; optionsBuilder.UseSqlite($"Filename={dbPath}"); } } } namespace Test.Mocks { public class MockInventoryFacade : InventoryFacade { public MockInventoryFacade(MockDatabaseContext databaseContext) : base(databaseContext) { } } } 
0
Dec 27 '17 at 3:41 on
source share

There is no IDbContext, because it would be useless, its only implementation would be DbContext.

The EF team also goes this way with IDbSet if you look at this project design note

For me, the real problem with EF when it comes to unit testing is the DbConnection in DbContext, fortunately there is Effort a good codeplex project that starts to populate this.

Effort is a powerful tool that allows you to create automated tests for applications based on the Entity Framework. This is mainly an ADO.NET provider that performs all operations with data in a lightweight main memory database during operation instead of a traditional external database. It also provides some intuitive helper methods that really make it easier to use this provider with existing ObjectContext or DbContext classes. A simple addition to existing code may be enough to create data-driven tests that can run without an external database.

With this, you can leave your DbContext and DbSet as is, and it is easy to run your unit tests. The only drawback to this is the difference between the Linq providers, where some unit tests can pass with effort rather than with a real backend.

UPDATE with EF7

I still claim that IDbContext is useless and the problem comes from DbConnection.

EF7 will also not have an IDbContext to perform the unit testing that the provider now provides in memory.

You see how Rowan Miller makes a demo: Modern data applications with Entity Framework 7

-10
Sep 11 '13 at 22:39
source share



All Articles