What calls the setters in an Entity?

前端 未结 2 1108
长情又很酷
长情又很酷 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 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 (this includes IDbSet).

    It then loops through this collection and providing the property isn't marked with a SuppressDbSetInitializationAttribute, it assigns an instance of a DbSet by invoking the DbContext's Set method and assigning the result.

    You can view the DbSetDiscoveryService code here.

提交回复
热议问题