当我实例化从EF DbContext派生的类时会发生什么? [英] What happens when i instantiate a class derived from EF DbContext?

查看:54
本文介绍了当我实例化从EF DbContext派生的类时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR;
您能总结一下当我实例化扩展EF DbContext的类的实例时会发生什么吗?

TL;DR;
Can you summarise what happens when I instantiate a instance of class that extends EF DbContext???


我想学习尽可能多的知识,并且由于对接口和抽象类的各种引用,仅阅读源代码的效率不是很高.

Long
I want to learn as much as I can about it and only reading the source code is not being very productive because of the various reference to interfaces and abstract classes...

我希望一步一步来,我将能够更轻松地确定正在处理的部分...

I was hoping that with a step by step, I would be able to more easily identify which part am dealing with...

我想为这个项目做贡献,我想对所有事情都有一个很好的了解...

I would like to contribute with the project and I want to have a sound understanding of everything...

那么,您能描述一下我实例化这样的一个类时发生的情况吗?:

So, can you describe what happens when I instantiate a class such as this one bellow?:

public class ProductContext : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

修改1
让我举例说明我正在寻找的答案类型,以发动汽车"为例:
首先,将钥匙插入插座.转动钥匙实际上是一个三步操作.稍稍转动一下即可验证钥匙是否适合门锁并由内部电池供电,汽车将启动电气系统,核算油量,关闭外部照明灯(如断电灯和前照灯),并关闭空调并转换一台内部电动发动机的所有动力,这将使主发动机转动.
如果再转动一点,同步系统将与气泵和电动发动机一起启动,导致曲轴旋转并使活塞运动,这是常规操作的反向模式.如果设置"了任何档位,汽车将行驶,并且主机启动将失败.如果不是这样,则气泵将在活塞腔中喷射气体,同步系统将产生火花,探查气体并启动主机.在此之后,电动机将停止运转,变压器将开始工作以补充主电池.
如果同步系统和气泵继续正常运行,则活塞中的点火将继续以4循环方式发生,汽车将准备好行驶.如果不是这样,汽车会死掉,整个过程可以重复.

edit 1
Let me exemplify the kind of answer i'm looking for, using as a example, "starting a car":
First you insert the key in the socket. Turning the key is actually a 3 steps action. When you turn it just a bit, validating that the key fits the lock and powered by the internal battery, the car starts electrical systems, accounting the amount of gas, turns off external lights like break-lights and headlights, turns off A/C and reverts all the power for a internal electric engine, that will turn the main engine.
if you turn it a bit more, sync systems will fire up, alongside the gas pump and the electrical engine, causing the crankshaft to spin and put the pistons in motion, which is the reverse mode of regular operation. If any gears are "set" the car will move and the main engine start will fail. If not the gas pump will spray gas in the piston chamber, the sync system will cause a spark, exploring the gas, and starting the main engine. After that the electric engine will stop, a transformer will enter in action replenishing the main battery.
If the sync system and gas pump continue to function flawlessly, the ignition in the pistons will continue to happen in a 4-cycle manner and the car will be ready to drive. If not the car will die, and the whole process can be repeated.

现在请注意,尽管没有提到完整"和技术"方面的各个方面,例如机油,水冷,发动机的机械内部,齿轮箱等……但是如果您有能力的话PLS可以做到!

Now pls note that although "complete" and "technical" various points were not noted, such as oil, water-cooling, mechanics internals of the engine, gear box and others... but if you are capable of doing so PLS do it!

推荐答案

我不太确定您要查找的内容,但是此信息可能会有所帮助.实例化 DbContext 类时,构造函数将调用一个私有方法,该方法将初始化 DbSet 属性.它通过使用名为 DbSetDiscoveryService 的内部类通过反射来发现当前类中的 DbSet 属性.

I'm not really sure what you're looking for but maybe this information will help. When you instantiate a DbContext class the constructor calls a private method that initializes the DbSet properties. It does this by using an internal class named DbSetDiscoveryService to discover via reflection the DbSet properties in the current class.

一些有用的链接:

  • 此处上的类似问答..li>
  • 此处是指向该 DbSetDiscoveryService 类.
  • 此处是实体框架源代码的链接.
  • A similar question and answer on SO here.
  • Here is a link to that DbSetDiscoveryService class.
  • Here is a link to the source code for the Entity Framework.

这篇关于当我实例化从EF DbContext派生的类时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆