首页 文章

无法加载实体框架提供程序类型?

提问于
浏览
371

我正在尝试在我的机器上安装的TeamCity上运行我的测试 .

System.InvalidOperationException:实体框架提供程序为'System.Data.SqlClient'ADO键入'System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089' . 无法加载NET提供程序 . 确保提供程序程序集可用于正在运行的应用程序 . 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882 .

我在我的任何项目中都没有提到 System.Data.Entity ,就像在codeplex上建议升级到EF6一样 .

所以,我不知道为什么我会得到这个例外 . 当我从VS运行测试时,我没有得到任何这样的异常 .

我确实尝试将CopyLocal设置为false然后再次设置为true ..但这似乎也不起作用 .

更新

我的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>

我在teamcity中得到以下stacktrace .

[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

30 回答

  • 2

    我不希望在我的应用程序项目中引用EF(或手动复制任何东西),所以我将其添加到我的EF项目的构建后事件中:

    cp $(TargetDir)EntityFramework.SqlServer.dll $(SolutionDir){your-main-bin-folder}
    
  • 97

    晚到派对,但最高投票的答案对我来说似乎都是黑客 .

    我所做的就是从测试项目中的app.config中删除以下内容 . 工作 .

    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    
  • 0

    我在程序集初始化类中用[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());
            }
        }
    }
    
  • 4

    除了这些有用的建议之外,如果您使用的是EF 6.1.3,请确保您的项目的.net版本为4.5或更高版本 .

  • 1

    确保EntityFramework.dll,EntityFramework.SqlServer.dll和您的提供程序的dll(对于SQL Server Compact,即EntityFramework.SqlServerCompact.dll)位于已部署的应用程序文件夹中 . 这对部署的应用程序尤为重要 .

  • 19

    我终于解决了这个问题事实证明,我在我的存储库类中错误地实现了IDIsposable . 我修好了 . 错误的实现导致了stackoverflow异常,因为我没有正确地处理掉资源 . 这导致VS不运行测试并且测试执行引擎崩溃 .

    我在这里向微软提交了这个(这是在我得到正确的解决方案之前) . connect.microsoft.com/VisualStudio/feedback/details/775868/vs-test-execution-crashes-in-vs-2012#details

    无论如何,这些构建现在在teamcity上运行良好 . 虽然,我仍然很好奇为什么VS Test执行引擎都没有一种优雅的方式告诉我发生了什么,而不是Team City .

    我通过手动调试测试找到了根本原因(我在这么多天后才意识到,修复花了我5秒钟) .

    希望这将有助于遇到此类问题的人 .

  • 0

    我有同样的isssue我试过多次,但它没有解决,但当我安装包EntityFramework.SqlServerCompact它解决了从Nuget包管理器安装此包 .

    Install-Package EntityFramework.SqlServerCompact
    
  • 8

    当我检查问题时,我注意到输出文件夹中缺少以下dll . 如果应用程序处于调试模式,则简单的解决方案是将Entityframework.dll和Entityframework.sqlserver.dll与app.config一起复制到输出文件夹 . 同时更改,app.config的构建选项参数“复制到输出文件夹”始终复制 . 这将解决您的问题 .

  • 39

    我检查了单元测试项目中的Debug Output窗口 . 未加载EntityFramework.SqlServer.dll . 将其添加到bin文件夹后,测试成功运行 .

  • 6

    我正在离线处理Contoso大学教程,在尝试使用EF创建第一个控制器时遇到了同样的问题 . 我不得不使用程序包管理器控制台从nuget缓存加载EF并创建一个到我的本地SQL Server实例的连接字符串,我的观点是我的web的webConfig设置可能没有设置为你所有人但我能够通过完全删除“entityFramework”中的“providers”部分来解决我的问题

    罗伯特

  • 0

    将Entityframework.dll和Entityframework.sqlserver.dll添加到参考项目解决了这个问题 .

  • 2

    通过nuget从项目中删除实体框架,然后将其添加回来 .

  • 4

    我看到类似的问题,并使用这篇文章中的方法:(http://entityframework.codeplex.com/workitem/1590),它解决了我的问题 .

    要解决此问题,可以通过在测试程序集中的任何位置添加这样的行来使测试程序集直接引用提供程序程序集:var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

  • 0

    我通过手动将 EntityFramework.SqlServer.dll 文件复制到主应用程序的 bin folder 来解决了这个问题 .

  • 0

    在我的情况下,我在以前安装SQL Server Express 2012(x64)时通过安装SQL Server 2012 Developer Edition解决了这个问题 . 似乎为我提供了缺失的依赖 .

  • 0

    只需参考或浏览EF dll - EntityFramework.SqlServer.dll

  • 0

    这只发生在我的负载/单元测试项目中 . 令人沮丧的是,我在一个已经运行了2年的项目中突然出现了 . 必须有一些测试运行的顺序打破了事情 . 我想一旦那个fi被删除它就消失了 .

    我发现简单地声明一个使用正确值的变量可以解决问题...我甚至都没有调用该方法 . 只需定义它 . 奇怪,但它的工作原理 .

    /// <summary>
    ///这样测试运行器就可以复制未被集成项目直接引用的dll
    /// </ summary>
    private void referenceLibs()

  • 1

    同样的问题,但我通过Nuget安装了EF 6 . 另一个可执行文件缺少EntityFramework.SqlServer . 我只是将nuget包添加到该项目中 .

  • 0

    我刚才有同样的错误信息 .

    我有一个单独的数据访问项目 . 在本地运行Web项目(引用数据项目)工作得很好 . 但是,当我部署Web项目以对程序集进行azure时:未复制EntityFramework.SqlServer . 我刚刚添加了对web项目的引用并重新部署,现在它可以工作了 .

    希望这有助于他人

  • 2

    我有问题,因为我没有添加对EntityFramework.sqlServer.dll的引用 . 当我开发程序时,它可以工作 . 但是当我发布应用程序并安装它时,它会抛出错误 .

    我只是再次添加引用和Build and Publish .

    References

  • 3

    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;
    }
    
  • 1

    我通过在DBContext类的顶部添加一个using stament来解决这个问题,如下所示:

    using SqlProviderServices= System.Data.Entity.SqlServer.SqlProviderServices;
    
  • 385

    我的解决方案是通过nuget管理器从项目中删除实体框架并将其重新添加 .

  • 24

    我的问题是,为了捕获另一个异常,我启用了公共语言运行时(CLR)异常 . 我忘了禁用它 .

    我在我的例外设置中禁用了它 . 它忽略了这个异常并继续运行并自动为我创建一个db(在我的例子中) .

  • 0

    我也有类似的问题

    通过执行以下操作解决了我的问题:

    enter image description here

    enter image description here

  • 14

    我在测试项目中遇到了同样的问题 - 我通过NuGet安装了最新的EF6位,每次我调用与EF相关的东西时:

    无法加载“System.Data.SqlClient”ADO.NET提供程序的Entity Framework提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer” . 确保提供程序程序集可用于正在运行的应用程序 . 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882 .

    我的解决方法:我将此方法放在我的测试项目中:

    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;
    }
    

    从未调用此方法,但我认为编译器将删除所有“不必要的”程序集,而不使用EntityFramework.SqlServer,测试失败 .

    无论如何:在我的机器上工作;)

    注意:Instead of adding the method to test project you can ensure a static reference to SqlProviderServices from your Model/entity project .

  • 253

    在尝试了所有其他建议的解决方案并且没有让我的项目工作之后,我终于在this page找到了一些评论:

    删除BIN-Folder为我做了

    它对我也有用 .

    现在我不知道你是否只需要删除bin文件夹它已经可以工作了,或者首先你必须做其他开发人员建议的事情,但重点是我只有在删除后才能使用它bin文件夹,这经过3个小时或更长时间寻找并尝试其他解决方案 . 所以,也许它对你也有用 .

  • 2

    在我的情况下 dll 没有被复制,虽然我添加了对它的引用 . 这是因为 EntityFramework.SqlServer.dll 未复制到您的项目中 . 添加该DLL,它将有希望工作 . 您可以从添加datamodel的项目中找到它 .

  • 4

    我已经为提供商使用了基于代码的注册 . link1 link2

    刚刚创建了配置类

    class DbContextConfiguration : DbConfiguration
    {
        public DbContextConfiguration()
        {
            this.SetDatabaseInitializer(new DropCreateDatabaseAlways<MyDbContext>());
            this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }
    

    关键点是 this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);

    并以这种方式使用它

    [DbConfigurationType(typeof(DbContextConfiguration))]
    public class MyDbContext : DbContext
    {
        public MyDbContext()
        {
            ...
        }
    
        public DbSet<...> ...{ get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            ...
        }
    }
    
  • 0

    有一个简单的解决方案 . 打开项目中的引用,右键单击“System.Data” - >属性 . 将“复制本地”更改为“True” .

    问题应该修复 .

相关问题