c# - Entity Framework 重用连接

标签 c# entity-framework

在 using block 中用于连接到实体,

调用另一个方法时如何重用连接?

所以我有:

using (SampleEntities entities = new SampleEntities()) {
    entities.Connection.Open();
    SomeMethod();

    ...
}


void SomeMethod() {
   using (SampleEntities entities = new SampleEntities())
   {
      // I want to be able to use the existing connection before the method call, any ideas?
   }

}

最佳答案

为什么没有

 void SomeMethod(SampleEnities context)

并传入;让第一个“使用”来解决处置问题。

关于c# - Entity Framework 重用连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2608509/

相关文章:

c# - 复杂的 LINQ 'Any' 查询

asp.net-mvc - 相对于我的模型,我应该有多少个 DbContext 子类?

c# - 实体类型 'Access' 需要定义一个主键。如果您打算使用无键实体类型调用 'HasNoKey()'

c# - 从C#中的存储过程中获取位值

c# - 如何使用 Entity Framework Core 将属性设置为 "Identity"或自动递增的列?

c# - Entity Framework 4.1、Generic Repository 和 ObservableCollection

c# - 有哪些用 C# 编写的 3D 游戏?

c# - 如何在wpf应用程序中显示网页的信息

c# - 如何通过 Visual Studio 在本地将 https 与 Azure Functions 结合使用?

c# - 清理方法中出现意外的 "Cannot access a disposed object"