'Adding dynamically a DbSet<TEntity> in DbContext class

Is there a way to add DbSet<TEntity> Property in DbContext class at runtime without passing through OnModelCreating ?



Solution 1:[1]

You can do it by dynamically creating a new type that inherits from DbContext and the use Reflection Emit to add the DbSets to this type.

It's a bit of a long solution to try and post into a response here, but here's a GitHub link to a working demo of how it can be done

EF Core Dynamic Db Context

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Filip Gorgievski