Let's say I'm developing code for a school, and I have SchoolDbContext . In most Entity Framework documentation, you can infer from DbContext :
public class SchoolDbContext : DbContext { public IDbSet<Student> Students => Set<Student>(); }
But my SchoolDbContext argument SchoolDbContext never a specialization of DbContext , instead it just uses DbContext , so in my opinion, SchoolDbContext should be composed of DbContext :
public class SchoolDbContext { private readonly DbContext _dbContext; public SchoolDbContext(DbContext dbContext) { _dbContext = dbContext; } public IDbSet<Student> Students => _dbContext.Set<Student>(); }
In my ASP.NET MVC application root directory, I tried this composite approach, setting up my dependencies like this (e.g. using Simple Injector):
private static void RegisterDependencies(Container container) {
Web.config:
<connectionStrings> <add name="SchoolDbContext" providerName="System.Data.SqlClient" connectionString="Server=(localdb)\MSSQLLocalDB;Integrated Security=true"/> </connectionStrings>
This fails when I try to load Students using
The Student object type is not part of the model for the current context.
When I change it to an inheritance approach (i.e. by calling the default constructor new SchoolDbContext() ), everything works fine.
Is my composition approach unsupported by the Entity Framework?
source share