c# - 为什么需要在客户端项目中引用 EntityFramework.dll 来使 DbContext IDisposable?

标签 c# .net entity-framework

创建一个包含您的 Entity Framework 模型和对象上下文的类库。然后向解决方案添加一个新的控制台应用程序。在控制台应用程序中,引用具有您的模型的项目。

现在在控制台应用程序中输入:

static void Main(string[] args)
{
    using (var context = new ExperimentalDbContext())
    {

    }
    Console.ReadKey();
}

构建时,您会收到错误报告:

The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly EntityFramework...yada yada yada...

现在,在过去的几年里我已经这样做了很多次,但每次出现这个错误时,我都会再次无奈地在互联网上搜索我当时已经忘记的解决方案。

解决此问题需要您在 ConsoleClient 项目中安装 EntityFramework NuGet 包。

所以,我的问题不是修复是什么,而是为什么?因为它没有任何意义!

为了完整起见,我使用的是 Entity Framework v6.1.3。但多年来,我在早期版本中也多次看到此错误。

更新

问题似乎只出现在您使用 using 时用于调用 Dispose 的代码块在 IDisposable

为了检验假设,创建一个控制台应用程序,它在同一解决方案中引用 ClassLibrary1,在同一解决方案中引用 ClassLibrary2,代码如下:

using ClassLibrary1;
using System;

namespace TestHypothesis1
{
    class Program
    {
        // Testing the hypothesis presented in this answer: http://stackoverflow.com/a/38130945/303685
        // This seems to be the behavior with just (or may be even others I haven't tested for)
        // IDisposable.
        // anotherFoo instance is created just fine, but the moment I uncomment
        // the using statement code, it shrieks.
        static void Main(string[] args)
        {
            //using (var foo = new Foo())
            //{
            //    foo.Gar = "Gar";

            //    Console.WriteLine(foo.Gar);
            //}

            var anotherFoo = new Foo() { Gar = "Another gar" };
            Console.WriteLine(anotherFoo.Gar);

            Console.ReadKey();
        }
    }
}


using ClassLibrary2;
using System;

namespace ClassLibrary1
{
    public class Foo: Bar, IDisposable
    {
        public string Gar { get; set; }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}


namespace ClassLibrary2
{
    public class Bar
    {
        public string Name { get; set; }
    }
}

并且您会观察到编译器仅针对第一个 Foo 的实例化提示缺少引用而不是第二次。

奇怪的是,在第一个 EntityFramework 示例中,如果您从控制台应用程序中删除了对 EntityFramework.dll 的引用并更改了 Main 中的代码对此,它仍然提示缺少引用。

static void Main(string[] args)
{
    var context = new ExperimentalDbContext();
    Console.ReadKey();
    context.Dispose();
}

此外,如果您注释掉对 context.Dispose() 的调用,上面代码片段的最后一行,即使抛出 InvalidOperationException 代码仍然可以正常工作但是,我推测,这是由于上下文的竞争条件在其迭代器完成其 MoveNext 之前被处理。打电话。

static void Main(string[] args)
{
    var context = new ExperimentalDbContext();
    Console.ReadKey();
    // context.Dispose();
}

因此,新的附加问题现在变为:

using的方式是什么?是否实现了使编译器停止链接引用的语句?

原来的问题也依然存在。

另一个更新

现在看来问题可能会进一步归零到对 IDisposable.Dispose 的调用方法,因此问题不在于 using 的实现。陈述。 using声明似乎只是一个无辜的保证 Dispose将被调用,仅此而已。

因此,在上面Foo例如,如果您插入对 anotherFoo.Dispose 的调用最后,编译器又开始提示了。像这样:

using ClassLibrary1;
using System;

namespace TestHypothesis1
{
    class Program
    {
        // Testing the hypothesis presented in this answer: http://stackoverflow.com/a/38130945/303685
        // This seems to be the behavior with just (or may be even others I haven't tested for)
        // IDisposable.
        // anotherFoo instance is created just fine, but the moment I uncomment
        // the using statement code, it shrieks.

        // Final update:
        // The trigger for the error seems to be the call to the Dispose method and not
        // particularly the implementation of the using statement, which apparently, simply
        // ensures that Dispose is called, as is also well-known and documented.
        static void Main(string[] args)
        {
            //using (var foo = new Foo())
            //{
            //    foo.Gar = "Gar";

            //    Console.WriteLine(foo.Gar);
            //}

            var anotherFoo = new Foo() { Gar = "Another gar" };
            Console.WriteLine(anotherFoo.Gar);

            anotherFoo.Dispose();

            Console.ReadKey();
        }
    }
}

所以,最后一个问题,那么,总结起来是这样的:

为什么调用Dispose阻止编译器链接程序集引用?

我认为我们现在有所进展。

最佳答案

原始答案

我不认为它是特定于 DbContext 的,但或多或​​少是因为在您的类库中引用的相关 DLL 没有转移到控制台应用程序。因此,当您构建时,编译器只知道控制台应用程序中的引用,而不是对 EntityFramework 的链接引用。它提示的唯一原因是因为编译器使用 using 语句运行检查以确保该类是 IDisposable,并且它知道的唯一方法是如果它解析 EntityFramework 库中的引用。

更新

事实证明,我仍然认为这是对的。如果在您的示例中,您忘记了 IDisposable,只是简单地尝试在控制台应用程序的 Bar 类上使用属性 Name,您会发现出现异常它不知道该属性,因为它位于未引用的程序集中。

未引用的程序集错误示例:

(inside Main)
Console.WriteLine(anotherFoo.Name);

就其值(value)而言,您实际上可以引用具有嵌套引用的库,并且永远不会在您的应用程序中包含这些嵌套引用,只要调用代码实际上从未到达引用或需要嵌套库的代码路径。这可能是错误的一个简单来源,特别是对于部署/发布场景。想象这样一种情况,您的发布不包含应用程序所需的所有库,但很少调用需要深度嵌套库的代码路径。然后有一天,你接到一个电话,说“应用程序坏了!”并且有人会立即说“但是什么都没有改变!自上次以来我们还没有部署!”这是在测试、QA、部署后等期间具有良好代码覆盖率的重要原因之一。

关于c# - 为什么需要在客户端项目中引用 EntityFramework.dll 来使 DbContext IDisposable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38130861/

相关文章:

c# - C# 中何时声明成员方法

c# - 如何查找System.Private.CoreLib.ni.dll中发生 'System.Runtime.InteropServices.SEHException'的原因?

c# - 如何剪切字符串的第一部分并存储它

c# - LINQ 中的自连接与聚合查询

c# - 为什么 SQLite 告诉我 "No current row"?

c# - 重型构造函数或使用方法

c# - silverlight/wpf 网页浏览器编码

.net - 设置仅在从 Visual Studio 而非构建服务器构建项目时执行的构建步骤

c# - Entity Framework Core 表拆分方法

c# - EntityContainer 名称在不同的程序集中必须是唯一的?