gpt4 book ai didi

c# - 管理 EntityConnection 生命周期

转载 作者:太空狗 更新时间:2023-10-29 20:15:13 24 4
gpt4 key购买 nike

关于管理 EntityContext 生命周期有很多问题,

例如Instantiating a context in LINQ to Entities

我得出的结论是,实体上下文应被视为一个工作单元,因此不应重复使用。太好了。

但是在为加快我的数据库访问速度做一些研究时,我遇到了这篇博文...

Improving Entity Framework Performance

该帖子认为,与其他框架相比,EF 的性能较差通常是由于每次需要新的 EntityContext 对象时都会创建 EntityConnection 对象。

为了对此进行测试,我在 Global.asax.cs Application_Start() 中手动创建了一个静态 EntityConnection。

然后我将所有上下文using语句转换为

using( MyObjContext currContext = new MyObjeContext(globalStaticEFConnection)
{
....
}

据我所知,这似乎加快了速度,没有任何错误。

但是这样安全吗?

使用应用程序范围内的静态 EntityConnection 会引入竞争条件吗?

最好的问候,凯文

最佳答案

EntityConnection is documented to be not thread-safe .我认为您可以将它们组合在一起,但是您不能为 Web 应用程序使用单个静态连接,因为这将涉及许多线程。

关于c# - 管理 EntityConnection 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com