C# 无法加载实体框架提供程序类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14033193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework Provider type could not be loaded?
提问by ashutosh raina
I am trying to run my tests on TeamCity which is currently installed on my machine.
我正在尝试在当前安装在我的机器上的 TeamCity 上运行我的测试。
System.InvalidOperationException:The Entity Framework provider type '
System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer,Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. Make sure the provider assembly is available to the running application.See http://go.microsoft.com/fwlink/?LinkId=260882for more information..
System.InvalidOperationException:实体框架提供程序类型“为” 'ADO.NET提供程序无法加载。确保提供程序程序集可用于正在运行的应用程序。
System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServerVersion=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'System.Data.SqlClient
I have no reference to System.Data.Entityin any of my projects as was suggested on codeplex for upgrading to EF6.
我System.Data.Entity在我的任何项目中都没有提到在 codeplex 上建议升级到 EF6。
So, I am not sure why am I getting this exception. I do not get any such exception when I run the tests from VS.
所以,我不确定为什么我会收到这个例外。当我从 VS 运行测试时,我没有遇到任何此类异常。
I did try to set CopyLocal to false then again to true.. but that does not seem to work either.
我确实尝试将 CopyLocal 设置为 false 然后再次设置为 true .. 但这似乎也不起作用。
Update
更新
My app.config has the following . Does this cause some behavior that I don't understand ?
我的 app.config 有以下内容。这是否会导致一些我不明白的行为?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
I get the following stacktrace in teamcity .
我在 teamcity 中得到以下堆栈跟踪。
[MSTest] IntegrationTests.CrudTest+QuestionTest.Create
[03:59:11][IntegrationTests.CrudTest+QuestionTest.Create] Initialization method IntegrationTests.CrudTest+QuestionTest.Initialize threw exception. System.InvalidOperationException: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. Make sure the provider assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information..
[03:59:11]
[IntegrationTests.CrudTest+QuestionTest.Create] at System.Data.Entity.Config.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
at System.Data.Entity.Config.ProviderServicesFactory.GetInstanceByConvention(String providerInvariantName)
at System.Data.Entity.Config.DefaultProviderServicesResolver.GetService(Type type, Object key)
at System.Data.Entity.Config.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Config.CachingDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Config.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Config.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Config.RootDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Config.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Config.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Config.CompositeResolver`2.GetService(Type type, Object key)
at System.Data.Entity.Config.IDbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
at System.Data.Entity.Config.InternalConfiguration.GetService[TService](Object key)
at System.Data.Entity.Config.DbConfiguration.GetService[TService](Object key)
at System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderServices(DbProviderFactory factory)
at System.Data.Entity.Infrastructure.DefaultManifestTokenService.GetProviderManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at EFRepository.Infrastructure.EFRepository`1.Add(T item) in c:\TeamCity\buildAgent\work\da2ea4e72c0e77f0\Repository\Infrastructure\EFRepository.cs:line 22
at IntegrationTests.CrudTest.Initialize() in c:\TeamCity\buildAgent\work\da2ea4e72c0e77f0\IntegrationTests\CrudTest.cs:line 34
采纳答案by Zapacila
Same problem, but i installed EF 6 through Nuget. EntityFramework.SqlServer was missing for another executable. I simply added the nuget package to that project.
同样的问题,但我通过 Nuget 安装了 EF 6。另一个可执行文件缺少 EntityFramework.SqlServer。我只是将 nuget 包添加到该项目中。
回答by ashutosh raina
I finally resolved this. Turns out, I had an erroneous implementation of IDIsposable in my repository class. I fixed that. The erroneous implementation caused a stackoverflow exception since I wasn't disposing off resources properly. This caused VS not to run the tests and the test execution engine crashed.
我终于解决了这个问题。事实证明,我在我的存储库类中错误地实现了 IDisposable。我修好了。错误的实现导致了计算器溢出异常,因为我没有正确处理资源。这导致 VS 无法运行测试并且测试执行引擎崩溃。
I filed it with Microsoft here ( this was before I got the correct solution). connect.microsoft.com/VisualStudio/feedback/details/775868/vs-test-execution-crashes-in-vs-2012#details
我在这里向微软提交了它(这是在我得到正确解决方案之前)。connect.microsoft.com/VisualStudio/feedback/details/775868/vs-test-execution-crashes-in-vs-2012#details
Anyway, the builds now run fine on teamcity. ALthough, I am still curious why neither VS Test execution engine had a graceful way of telling me what was happening not Team City.
无论如何,构建现在在 teamcity 上运行良好。尽管如此,我仍然很好奇为什么 VS Test 执行引擎没有一种优雅的方式告诉我发生了什么而不是 Team City。
I discovered the root cause by manually debugging the test ( which I only realised after so many days , the fix took me 5 seconds).
我通过手动调试测试发现了根本原因(我在这么多天后才意识到,修复花了我 5 秒钟)。
Hopefully this will help someone who comes across such issues.
希望这会对遇到此类问题的人有所帮助。
回答by Robert Muehsig
I had the same problem in my Test projects - I installed the latest EF6 bits via NuGet and everytime I invoke something EF-related I got:
我在我的测试项目中遇到了同样的问题——我通过 NuGet 安装了最新的 EF6 位,每次我调用与 EF 相关的东西时,我都会得到:
The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. Make sure the provider assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882for more information.
无法加载“System.Data.SqlClient”ADO.NET 提供程序的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer”。确保提供程序程序集可用于正在运行的应用程序。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=260882。
My workaround: I placed this method inside my test project:
我的解决方法:我将此方法放在我的测试项目中:
public void FixEfProviderServicesProblem()
{
//The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
//for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.
//Make sure the provider assembly is available to the running application.
//See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
}
This method is never been called, but I think the compiler will remove all "unnecessary" assemblies and without using the EntityFramework.SqlServer stuff the test fails.
从未调用过此方法,但我认为编译器将删除所有“不必要的”程序集,并且在不使用 EntityFramework.SqlServer 内容的情况下测试失败。
Anyways: Works on my machine ;)
无论如何:在我的机器上工作;)
回答by vkuschenko
I've used Code-based registration for provider. link1link2
Just created the configuration class like
刚刚创建了配置类
class DbContextConfiguration : DbConfiguration
{
public DbContextConfiguration()
{
this.SetDatabaseInitializer(new DropCreateDatabaseAlways<MyDbContext>());
this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
}
}
Key point is this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
关键是this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
and used it in the such way
并以这种方式使用它
[DbConfigurationType(typeof(DbContextConfiguration))]
public class MyDbContext : DbContext
{
public MyDbContext()
{
...
}
public DbSet<...> ...{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
}
}
回答by Timothy Schoonover
Nuget will configure your EF6 project to reference EntityFramework.SqlServer.dll. This deploys to the output folder for your EF6 project during build, but it will not deploy to the output folder for projects referencing your EF6 project. I believe this is because Visual Studio is "smart" enough to detect that nothing in your assembly is actually using the dll directly and so does not include it. You can force EntityFramework.SqlServer.dll to be deployed to the output folder of projects referencing your EF6 project (unit tests, UI's etc.) by adding code to your EF6 project that uses EntityFramework.SqlServer.dll. Be careful not to put the code in a generated class as you risk losing it on next regen. I chose to add the following class to the assembly, which fixed the problem.
Nuget 将配置您的 EF6 项目以引用 EntityFramework.SqlServer.dll。这会在构建期间部署到 EF6 项目的输出文件夹,但不会部署到引用 EF6 项目的项目的输出文件夹。我相信这是因为 Visual Studio 足够“聪明”,可以检测到您的程序集中实际上没有直接使用 dll,因此不包含它。通过将代码添加到使用 EntityFramework.SqlServer.dll 的 EF6 项目,您可以强制将 EntityFramework.SqlServer.dll 部署到引用您的 EF6 项目(单元测试、UI 等)的项目的输出文件夹。小心不要将代码放在生成的类中,因为您可能会在下次重新生成时丢失它。我选择将以下类添加到程序集中,从而解决了问题。
using System.Data.Entity.SqlServer;
internal static class MissingDllHack
{
// Must reference a type in EntityFramework.SqlServer.dll so that this dll will be
// included in the output folder of referencing projects without requiring a direct
// dependency on Entity Framework. See http://stackoverflow.com/a/22315164/1141360.
private static SqlProviderServices instance = SqlProviderServices.Instance;
}
回答by pastrami01
I just had the same error message.
我只是有同样的错误信息。
I have a separate project for my data access. Running the Web Project (which referenced the data project) locally worked just fine. But when I deployed the web project to azure the assembly: EntityFramework.SqlServer was not copied. I just added the reference to the web project and redeployed, now it works.
我有一个单独的数据访问项目。在本地运行 Web 项目(引用数据项目)工作正常。但是当我部署 web 项目到 azure 程序集时: EntityFramework.SqlServer 没有被复制。我刚刚添加了对 web 项目的引用并重新部署,现在它可以工作了。
hope this helps others
希望这对其他人有帮助
回答by Innovation Wang
I see similar problem, and using the method from this post: (http://entityframework.codeplex.com/workitem/1590), which solves my problem.
我看到了类似的问题,并使用了这篇文章中的方法:(http://entityframework.codeplex.com/workitem/1590),它解决了我的问题。
To work around the issue you can make your test assembly directly reference the provider assembly by adding some line like this anywhere in the test assembly: var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
要解决此问题,您可以通过在测试程序集中的任何位置添加一些类似这样的行,使您的测试程序集直接引用提供程序程序集: var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
回答by Robert K
I was working on the Contoso University tutorial offline and encountered the same issue when trying to create my first controller using EF. I had to use the Package Manager Console to load EF from the nuget cache and created a connection string to my local instance of SQL Server, my point here is my webConfig setting for EF may not be set as you all out there but I was able to resolve my issue by completely deleting the "providers" section within "entityFramework"
我正在离线学习 Contoso 大学教程,并在尝试使用 EF 创建我的第一个控制器时遇到了同样的问题。我不得不使用包管理器控制台从 nuget 缓存加载 EF 并创建了一个连接字符串到我的本地 SQL Server 实例,我的观点是我的 EF webConfig 设置可能没有像你们一样设置,但我能够通过完全删除“entityFramework”中的“providers”部分来解决我的问题
Robert
罗伯特
回答by Random
I sorted it out with [DeploymentItem] on my assembly initializing class
我在我的程序集初始化类上使用 [DeploymentItem] 对其进行了整理
namespace MyTests
{
/// <summary>
/// Summary description for AssemblyTestInit
/// </summary>
[TestClass]
[DeploymentItem("EntityFramework.SqlServer.dll")]
public class AssemblyTestInit
{
public AssemblyTestInit()
{
}
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[AssemblyInitialize()]
public static void DbContextInitialize(TestContext testContext)
{
Database.SetInitializer<TestContext>(new TestContextInitializer());
}
}
}
回答by ChinaHelloWorld
There is a easy fix. open the references in your project, right click "System.Data" -> properties. Change "Copy Local" to "True".
有一个简单的解决方法。打开项目中的引用,右键单击“System.Data”-> 属性。将“复制本地”更改为“真”。
Problem should be fixed.
问题应该得到解决。

