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?

- 37,465
- 35
- 132
- 205
2 Answers
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.

- 1,658
- 4
- 32
- 48

- 34,151
- 9
- 98
- 120
EF uses reflection to discover IDbSet properties on DbContext derived classes and sets them accordingly.

- 31,342
- 4
- 73
- 104