DbContext 是否在其生命周期中保持打开的连接?

Does DbContext holds an opened connection during its life-cycle?

问题很清楚了。 DbContext 是否在其生命周期中保持 打开连接 ? EF核心呢?

正如其他人所说,不,它不会,除非您手动打开连接并将其传递给 DbContext 构造函数。

可以在这里找到具体的详细答案

@ajcvickers 在https://github.com/aspnet/EntityFrameworkCore/issues/7810 中的评论清除了它:

If EF creates the DbConnection object, then EF will ensure it is disposed when the DbContext is disposed. On the other hand, if some other code creates the DbConnection object and passes it to EF, then it is the responsibility of the other code to also dispose the connection appropriately.

Similar rules apply to opening and closing the connection. If EF opens the connection, then EF will close the connection when it is done with it. If your code opens the connection, then your code should close the connection.