In Entity Framework, you must create a class that derives from DbContext
with IDbSet properties. What in Entity Framework calls the setters and how does this work?
EF uses reflection to discover IDbSet properties on DbContext derived classes and sets them accordingly.
When your custom context class is instantiated, the base DbContext
constructor calls a private method called InitializeLazyInternalContext
which in turn calls another private method called DiscoverAndInitializeSets
.
This method creates a new instance of a DbSetDiscoveryService
, passing in the current context as a constructor parameter, and then calls its InitializeSets
method. This method in turn calls GetSets
which uses reflection to get a list of any property on the derived context that is assignable from DbSet<T>
(this includes IDbSet<T>
).
It then loops through this collection and providing the property isn't marked with a SuppressDbSetInitializationAttribute
, it assigns an instance of a DbSet<T>
by invoking the DbContext's Set<TEntity>
method and assigning the result.
You can view the DbSetDiscoveryService
code here.