我正在尝试在TeamCity上运行我的测试,它目前安装在我的机器上。

系统。InvalidOperationException: 实体框架提供程序类型 “System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework。SqlServer,版本=6.0.0.0,区域性=中性, System.Data.SqlClient的PublicKeyToken=b77a5c561934e089 ADO。NET提供程序无法加载。确保提供程序集 对正在运行的应用程序可用。 详见http://go.microsoft.com/fwlink/?LinkId=260882 信息. .

我在codeplex上升级到EF6的任何项目中都没有引用System.Data.Entity。

我不确定为什么会得到这个异常。 当我从VS运行测试时,我没有得到任何这样的异常。

我尝试设置CopyLocal为假,然后再次为真..但这似乎也不管用。

更新

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

当前回答

在尝试了所有其他建议的解决方案,并没有让我的项目工作后,我终于在这个页面上找到了一个小评论:

删除bin文件夹帮我解决了这个问题

这对我也很有效。

其他回答

我在程序集初始化类上使用[deploymenttem]整理了它

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

这只发生在我的负载/单元测试项目中。令人沮丧的是,我在一个已经运行了2年的项目中突然出现了这种情况。一定是某些测试运行的顺序把事情搞砸了。我猜一旦fi被移除,它就消失了。

我发现,简单地声明一个变量,使用正确的值修复问题…我从来没有调用过这个方法。定义一下。很奇怪,但很有效。

> / / / <总结 ///测试运行程序复制不被集成项目直接引用的dll > / / / < /总结 private void referenceLibs() { var useless = SqlProviderServices.Instance; }

我已经创建了一个静态的“启动”文件,并添加了代码来强制DLL被复制到其中的bin文件夹中,作为分离这个“配置”的一种方式。

[DbConfigurationType typeof (DbContextConfiguration))) 公共静态类 { }

public class DbContextConfiguration : DbConfiguration
{
    public DbContextConfiguration()
    {
        // This is needed to force the EntityFramework.SqlServer DLL to be copied to the bin folder
        SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
    }
}

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

只需引用或浏览EF dll - EntityFramework.SqlServer.dll