How DbContext initializes automatic DbSet<T> properties?

若如初见. 提交于 2019-12-20 01:43:41

问题


Consider the following class:

class MyContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
}

and instantiating a new object:

var mycontext = new MyContext();

Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object.


回答1:


From looking at the reflected code, when the DbContext (the base class) is constructed it makes a call to the DbSetDiscoveryService (an internal clasS) - which essentially uses reflection to find all properties on the DbContext, and then initializes those that need initializing.

So in short - using reflection in the constructor.



来源:https://stackoverflow.com/questions/18210265/how-dbcontext-initializes-automatic-dbsett-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!