什么叫实体的设定者? [英] What calls the setters in an Entity?
问题描述
在实体框架中,您必须创建一个派生自 DbContext
与IDbSet属性的类。实体框架中的什么调用setter,这是如何工作的?
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?
推荐答案
当您的自定义上下文类被实例化时, c $ c> DbContext 构造函数调用私有方法调用 InitializeLazyInternalContext
,这又调用另一个私有方法调用 DiscoverAndInitializeSets
。
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
.
此方法创建一个 DbSetDiscoveryService
的新实例,将当前上下文作为构造函数参数,然后调用其 InitializeSets
方法。该方法又调用 GetSets
,它使用反射来获取派生上下文中可从 DbSet< T>
(这包括 IDbSet< T>
)
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>
).
然后它循环遍历此集合并提供属性没有标记一个 SuppressDbSetInitializationAttribute
,它通过调用DbContext的 DbSet< T>
code>设置< TEntity> 方法并分配结果。
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.
您可以查看 DbSetDiscoveryService
代码 here 。
这篇关于什么叫实体的设定者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!