Can a DbContext be compiled in place of an inherited one?

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) { // Assume I have a connection string SchoolDbContext container.Register(() => new DbContext("SchoolDbContext"), Lifestyle.Scoped); container.Register<SchoolDbContext>(Lifestyle.Scoped); } 

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?

+6
source share
1 answer

Can a DbContext be compiled in place of an inherited one?

Short answer: NO

To quote comments from official documentation (emphasis mine)

DbContext typically used with a derived type that contains the DbSet<TEntity> properties for the model root objects. These sets are automatically initialized when an instance of the derived class is created ....

There are more, but too many, to answer this question.

Source Class DbContext

Is my composition approach unsupported by the Entity Framework?

If you look at the source code for DbContext , there are internal methods that look for a class for DbSet and initialize them.

This section of code is basically

 /// <summary> /// Initializes the internal context, discovers and initializes sets, and initializes from a model if one is provided. /// </summary> private void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null) { DbConfigurationManager.Instance.EnsureLoadedForContext(GetType()); _internalContext = new LazyInternalContext( this, internalConnection, model , DbConfiguration.GetService<IDbModelCacheKeyFactory>() , DbConfiguration.GetService<AttributeProvider>()); DiscoverAndInitializeSets(); } /// <summary> /// Discovers DbSets and initializes them. /// </summary> private void DiscoverAndInitializeSets() { new DbSetDiscoveryService(this).InitializeSets(); } 

Any model you are trying to get will result in the same error that the model was not part of the context when it was initialized, which can only happen if it was a member of a derived class.

Github DbContext source code

+5
source

Source: https://habr.com/ru/post/1264967/


All Articles