为什么需要在客户端项目中引用EntityFramework.dll来使DbContext IDisposable ?
本文关键字:dll EntityFramework 来使 DbContext IDisposable 引用 客户端 项目 为什么 | 更新日期: 2023-09-27 17:53:44
创建一个包含实体框架模型和对象上下文的类库。然后向解决方案添加一个新的控制台应用程序。从控制台应用程序中,引用具有您的模型的项目。
现在在控制台应用程序中输入以下内容:
static void Main(string[] args)
{
using (var context = new ExperimentalDbContext())
{
}
Console.ReadKey();
}
当你构建时,你会得到一个错误报告:
类型为"System.Data.Entity"。DbContext'在程序集中定义没有被引用。必须添加对程序集的引用EntityFramework……Yada Yada Yada…
在过去的几年里,我已经做过很多次了,但每次我得到这个错误,我又一次无助地在网上搜索解决方案,我已经忘记了。
解决这个问题需要在ConsoleClient项目中安装EntityFramework NuGet包。
所以,我的问题不是关于修复是什么,而是为什么?因为这根本说不通!只是为了完整起见,我使用的是v6.1.3的实体框架。但是多年来,我在早期版本中也见过很多次这个错误。
似乎只有当您使用using
代码块时才会出现问题,该代码块意味着在IDisposable
s上调用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.
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);
就其价值而言,您实际上可以引用具有嵌套引用的库,并且在应用程序中从不包含这些嵌套引用,只要调用代码实际上从未到达引用或需要嵌套库的代码路径。这很容易产生bug,特别是对于部署/发布场景。想象一下这样一个场景:你的发布不包括应用程序所需的所有库,但需要深度嵌套库的代码路径很少被调用。然后有一天,你接到一个电话说:"应用程序坏了!"自从上次以来,我们还没有部署过!"这是在测试、QA、部署后等期间具有良好代码覆盖率的突出原因之一。
这不是对Dispose()
的特定调用,也不是与EF有关的任何事情。这只是编译器对任何引用程序集的工作方式:如果您想与当前项目不直接引用的程序集中定义的对象(读取:使用属性或方法)进行交互,则不能:您必须直接向该程序集添加引用。
这里发生的是你对Dispose()
的调用是从DbContext
调用的方法,因为你没有在你的第一级程序集中实现它。正如@poke指出的在某些情况下,您可以通过在类上实现调用另一个程序集的方法来轻松解决这个问题:只要您不试图直接访问它就可以。这可能在某些情况下对人们有效,但它不适用于EF上下文的特定情况。
问题是您的上下文将具有类型DbSet<your entity type>
的属性,其本身是对System.Data
中的System.Data.Entity
命名空间的引用。虽然我发现这阻止了与"第一级"对象的任何交互:它允许你创建一个新的对象,但是当你试图访问任何属性或方法时,你会得到相同的编译错误。我认为这是由于编译器优化了代码,因此创建但从未使用过的对象实际上从未被创建。
只是为了澄清你在更新中说的一些话:
using语句似乎只是一个无辜的保证,只会调用Dispose而不会调用其他操作。
这完全正确。你可以在规范的8.13中找到确切的细节,但一般用例是展开为
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
基本上只是将您的语句包装在try...finally
中,资源作用域为新块。这里没有魔法。如前所述,您可以通过在"一级"类上实现IDisposable
并在那里实现Dispose
来调用"二级"汇编来解决这个问题。在某些情况下,这可能会让你走得足够远。
下面是一些用更一般的方式询问这个问题的前面问题的例子:
- 为什么我必须链接引用程序集?
- 为什么我(有时)必须引用由我引用的程序集引用的程序集?
- 为什么编译器在另一个程序集中使用重载时有时要求您也引用子程序集?
// our first, base class. put this in it's own project.
namespace SecondLevel
{
public class SecondLevel
{
public void DoSomething()
{
}
}
}
// our second class that references the base class. Add this to it's own project as well.
namespace FirstLevel
{
public class FirstLevel
{
public SecondLevel.SecondLevel reference;
public FirstLevel()
{
reference = new SecondLevel.SecondLevel();
}
public void DoSomethingWithReferencedClass()
{
reference.DoSomething();
}
}
}
// our "Client" console app that does nothing, but indicates the compiler errors.
// also in it's own project
// Reference the project that "FirstLevel" is in,
// but not the one that "Second Level" is in.
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var primary = new FirstLevel.FirstLevel();
primary.reference.DoSomething(); // will cause compiler error telling you to reference "Second Level"
primary.DoSomethingWithReferencedClass(); // no compiler error: does the same thing the right way.
}
}
}
注意,primary.DoSomethingWithReferenceClass();
将调用相同的方法,但没有错误,因为它不直接尝试访问第二级类。
关于为什么是的问题,你必须问微软团队。解决方案是要么接受必须添加引用的事实,要么编写代码,使客户端只知道被引用程序集的单个级别。