What calls the setters in an Entity?

前端 未结 2 1084
长情又很酷
长情又很酷 2021-01-24 10:25

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?

相关标签:
2条回答
  • 2021-01-24 10:56

    EF uses reflection to discover IDbSet properties on DbContext derived classes and sets them accordingly.

    0 讨论(0)
  • 2021-01-24 11:08

    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.

    0 讨论(0)
提交回复
热议问题