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

ash*_*ina 406 c# mstest entity-framework teamcity-7.1 entity-framework-6

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

System.InvalidOperationException:

实体框架提供程序类型" 为" 'ADO.NET提供程序无法加载.确保提供程序程序集可用于正在运行的应用程序.System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServerVersion=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'System.Data.SqlClient

有关详细信息,请参阅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>
Run Code Online (Sandbox Code Playgroud)

我在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
Run Code Online (Sandbox Code Playgroud)

Zap*_*ila 415

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

  • 我也注意到了这一点.如果您创建使用EF的库,VS会将EF.dll和EF.SqlServer.dll放入该构建文件夹中.但是,如果您现在有另一个程序使用您的库,则只有EF.dll将被放入此构建文件夹中.缺少EF.SqlServer.dll.如果将其手动添加到构建文件夹,该程序将起作用.本身不是一个好的解决方案,但显示问题是EF.SQLServer.dll缺失是导致错误的原因. (103认同)
  • 我添加了var x = typeof(System.Data.Entity.SqlServer.SqlProviderServices); 然后我的应用工作 (44认同)
  • 当我输入代码时,我总是担心这完全没什么,而且突然间我的项目运行了.O_O (29认同)
  • 我觉得很烦人,EF 6.1现在需要在我的ASPX DLL/Project上安装EF,而EF 5.0只需要在我的数据层DLL/Project上使用它. (15认同)
  • 此外,由于编译器优化,您可能还需要执行`x.ToString()`,否则它将取消Release中的`typeof`. (10认同)
  • 我在Nuget中搜索了"EntityFramework.SqlServer"并且没有结果.包裹在哪里?未列在已安装的列表中 (5认同)
  • 您只是不希望所有项目都将EF作为参考,因为应用程序的Datalayer必须解决问题.Robert Muehsig的解决方案解决了EF中的错误.我将他的代码放在我的DBcontext文件中.即使有时甚至不可能将所有项目都包含在EF中,因为您没有构建调用数据层的所有项目. (3认同)

Rob*_*sig 270

我在我的测试项目中遇到了同样的问题 - 我通过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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

注意:您可以确保从模型/实体项目中对SqlProviderServices进行静态引用,而不是将该方法添加到测试项目中.

  • 我最终做了同样的事情.我想和那个使这个程序集依赖于打开db连接的人说话.在他们的备用Universe中,所有具有连接字符串的配置文件的前端项目都应该引用EntityFramework,只是为了让这个dll打开连接.这有什么意义我不明白. (24认同)
  • 需要说明的是:将上述代码添加到`DbContext`类将解决EF 6.1中的问题.这样,您就不需要在前端(WebApi等)项目中包含Entity Framework Nuget包,并且可以在数据层中保留与EF相关的所有内容. (7认同)
  • 魔法!但它适用于我的Integration Test项目.谢谢. (3认同)
  • 这是一个黑客,但它是我找到的最好/最简单的解决方案,你没有为你的项目创建一个部署包. (3认同)
  • 对于你们所有人使用其他建议,如"var x = typeof(SqlProviderServices);".只有Robert提供的这个解决方案才能用于开发和构建机器! (2认同)
  • 感谢Robert,EF团队很荒谬,但无论如何这对我有用. (2认同)

Tim*_*ver 105

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 小点,但最好在这个类中添加注释来解释为什么它是必要的. (3认同)
  • 这为我解决了关于 MSTest 的问题。我喜欢这种方法,因为它不需要我在测试类上对类继承做任何混乱的事情。只需在测试程序集和 MAGIC 中的某处包含该类。谢谢! (2认同)
  • 对于那些不想花4秒钟查看的人:使用System.Data.Entity.SqlServer; (2认同)

Kev*_*vbo 42

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

  • `PM> Update-Package -Reinstall"EntityFramework"-ProjectName"MyProj1"` (9认同)

小智 29

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

using SqlProviderServices= System.Data.Entity.SqlServer.SqlProviderServices;
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案在功能上与 [MissingDllHack](http://stackoverflow.com/a/22315164/1141360) 相同。它可能看起来更简洁,但在您的上下文类中引入了解决方法代码。此代码也将被标记为由 ReSharper 等工具删除,并且随着时间的推移很容易丢失。一个很好的解决方案,但我更喜欢以不产生代码警告的方式将解决方法与上下文分开。 (3认同)
  • 这是解决这个问题的最干净的方法。谢谢你。 (2认同)
  • 此解决方案有效且看起来很干净。问题:我们不会在 DBContext 类刷新时丢失 line id 吗?即使用 edmx 类生成? (2认同)

小智 20

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

刚刚创建了配置类

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

关键点是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)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)


Ran*_*dom 14

我在程序集初始化类中用[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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jer*_*ley 9

迟到了,但最高投票的答案对我来说都像是黑客.

我所做的就是从测试项目中的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>
Run Code Online (Sandbox Code Playgroud)


小智 8

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

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

参考


小智 5

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


小智 5

我遇到了同样的问题,我尝试了很多次,但没有解决,但是当我安装 EntityFramework.SqlServerCompact 包时,它解决了从 Nuget 包管理器安装此包的问题。

Install-Package EntityFramework.SqlServerCompact
Run Code Online (Sandbox Code Playgroud)