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?
In Entity Framework, you must create a class that derives from DbContext with IDbSet
Share
When your custom context class is instantiated, the base
DbContextconstructor calls a private method calledInitializeLazyInternalContextwhich in turn calls another private method calledDiscoverAndInitializeSets.This method creates a new instance of a
DbSetDiscoveryService, passing in the current context as a constructor parameter, and then calls itsInitializeSetsmethod. This method in turn callsGetSetswhich uses reflection to get a list of any property on the derived context that is assignable fromDbSet<T>(this includesIDbSet<T>).It then loops through this collection and providing the property isn’t marked with a
SuppressDbSetInitializationAttribute, it assigns an instance of aDbSet<T>by invoking the DbContext’sSet<TEntity>method and assigning the result.You can view the
DbSetDiscoveryServicecode here.