438

I am trying to run my tests on TeamCity which is currently installed on my machine.

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..

I have no reference to System.Data.Entity in any of my projects as was suggested on codeplex for upgrading to 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.

I did try to set CopyLocal to false then again to true.. but that does not seem to work either.

Update

My app.config has the following . Does this cause some behavior that I don't understand ?

<?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 .

[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
dampee
  • 3,248
  • 1
  • 18
  • 31
ashutosh raina
  • 8,700
  • 10
  • 40
  • 80
  • 1
    Is EntityFramework.SqlServer.dll deployed along with EntityFramework.dll? In EF6 SqlServer and SqlServerCE providers are not in the same assembly as the EF engine as it was with previous versions. You should not add a reference to the System.Data.Entity.dll. The provider model in EF6 changed compared to EF5 and EF5 provider will just not work. In addition you can hit a few tricky problems (like EF5 geospatial types treated as entity types in EF6) – Pawel Dec 27 '12 at 07:31
  • Yes I have double checked there is no reference to System.Data.Entity and the references to both EntityFramework.dll and EntityFramework.SqlServer.dll are present. Again the projetc compiles and tests run in VS. It is only when I the tests run on TeamCity that the error crops up. – ashutosh raina Dec 27 '12 at 07:47
  • This is a correct setup. I have not used TeamCity but it appears to me that when your app is deployed to run the tests with TeamCity the EntityFramework.SqlServer.dll is missing and hence the exception. – Pawel Dec 27 '12 at 07:49
  • I have removed bin from my gitignorelist .. it still throws the same error.. any ideas where it is trying to look for the dlls ? – ashutosh raina Dec 27 '12 at 08:08
  • Here is how the runtime locates assemblies - http://msdn.microsoft.com/en-us/library/yx7xezcf.aspx. I would just just start looking in the same folder where your app and the EntityFramework.dll on the TeamCity server is. – Pawel Dec 27 '12 at 08:44
  • @Pawel I have put the code up here https://github.com/ashutoshraina/Pariksha ..can you see if you can find the issue. The path to the test seems to be ok since TeamCity is picking it up correctly. – ashutosh raina Dec 27 '12 at 13:46
  • I did not spot anything wrong with your project. Can you share the stack trace? I wonder where the call originated from... – Pawel Dec 28 '12 at 00:55
  • No, this was not resolved. Tried all sorts of things. Yet to find a solution. – ashutosh raina Jan 11 '13 at 07:45
  • 1
    see the answer from this post: http://stackoverflow.com/questions/21175713/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name I added the `private volatile Type _dependency...` answer and it worked! It just stinks that I have to add a separate class like this just to get EF to work in TeamCity. – a11smiles May 19 '16 at 15:47
  • 2
    I uninstalled EntityFramework from nuget packages and installed again and it got fixed – otaku Nov 17 '16 at 10:31
  • You need to manually add the reference to these assemblies. EntityFramework, EntityFramework.SqlServer – kbvishnu Nov 12 '19 at 18:05

34 Answers34

440

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.

Narcis
  • 5,702
  • 3
  • 19
  • 29
  • 110
    I noticed this too. If you create a library that uses EF, VS will put the EF.dll and the EF.SqlServer.dll into that build folder. But if you now have another program make use of your library, only the EF.dll will be put into this build folder. The EF.SqlServer.dll is missing. If you add it manually to the build folder, the program works. Not a good solution in itself, but shows the problem is the EF.SQLServer.dll missing is the cause of the error. – Eric Oct 25 '13 at 17:21
  • 44
    I added var x = typeof(System.Data.Entity.SqlServer.SqlProviderServices); Then my app worked – Brian Dec 19 '13 at 20:03
  • 33
    I always worry when I type code in that does absolutely nothing and all the sudden my project runs. O_o – Jordan Apr 14 '14 at 20:07
  • 10
    Also, because of compiler optimization you might want to also do `x.ToString()` or it will take out the `typeof`s in Release. – Jordan Apr 15 '14 at 13:07
  • 15
    I find it annoying that EF 6.1 now needs EF installed on my ASPX DLL/Project, whereas EF 5.0 only needed it on my data-layer DLL/Project. – PeterX Apr 24 '14 at 06:27
  • 1
    Ended up just adding the EntityFramework.SqlServer.dll manually to the project from packages folder. – Alex M Jun 02 '14 at 14:32
  • 3
    You just don't want all your projects have EF as a reference, since the Datalayer of your application must solve the problem. The solution of Robert Muehsig solves the bug in EF. I placed his code in my DBcontext file. Even sometimes it's even not possible to include all projects with EF because you are not building all the projects which are calling the datalayer. – Mario Mar 10 '15 at 10:50
  • 5
    I searched in Nuget for "EntityFramework.SqlServer" and there are no results. Where is the package? Not listed in the installed list either – Hoppe Jul 14 '15 at 16:40
  • 1
    Incidentally, GC.KeepAlive is a better solution for "I need to reference something" than random variables and ToStrings – Mark Sowul Nov 12 '15 at 18:17
  • I cannot load EntityFramework.SqlServer.dll even if it is there in the bin directory (without me copying it to that location) – Anders Lindén Jun 07 '16 at 12:12
  • I needed to remove EF and then re add it for this to take. – mcfea Sep 01 '16 at 16:15
  • If anyone run into the problem after removing the EntityFramework.SqlServer reference from your project, just add it back. It should be located in *YourSolutionDir\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll*. But.. I'm just wondering why it is needed for EntityFramework even if we do not use SqlServer. – zwcloud Sep 07 '16 at 05:08
  • 1
    You may additionally need to add `[DeploymentItem("EntityFramework.SqlServer.dll")]` and `[DeploymentItem("EntityFramework.dll")]` to as attributes to your unit test class if the file is still not being copied after you add it as a reference. – codechurn Sep 30 '16 at 20:53
  • Add the same entity framework reference in config located project ,One situation I have faced , EF created in one class library and web config file located in my UI project, Add the same reference add UI then work fine – Rajeev Mar 21 '19 at 18:13
  • [The package is now unlisted](https://www.nuget.org/packages/EntityFramework.SqlServer/). It appears to have been a pre-release version that never made it to an official release? – kamilk Aug 01 '19 at 15:56
  • 1
    You need to manually add the reference to these assemblies. EntityFramework, EntityFramework.SqlServer – kbvishnu Nov 12 '19 at 18:05
  • 1
    1000 downvotes to microsoft- why my tester needs EF references ? – Dani Jul 14 '20 at 14:50
280

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:

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.

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.

Anyways: Works on my machine ;)

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

Ashkan Sirous
  • 7,211
  • 5
  • 41
  • 64
Robert Muehsig
  • 4,978
  • 2
  • 27
  • 30
  • 25
    I ended up doing the same. I would like to speak to that person who made this assembly a dependency on opening a connection to db. In their alternate universe all of our frontend projects that have config file with a connection string should reference EntityFramework just to get this one dll to open the connection. In what way does this make sense I do not understand. – juhan_h Oct 21 '13 at 10:19
  • 3
    It's a hack, but it's the best/easiest solution I've found for the scenario in which you don't have a deployment package created for your project. – Kon Dec 06 '13 at 22:05
  • 3
    For all of you using other suggestions like "var x = typeof(SqlProviderServices);". Only this solution provided by Robert works on development and build machines!!! – Alexander Schmidt Feb 08 '14 at 13:38
  • 3
    Thanks Robert, it is ridiculous by EF team, but anyway that is working for me. – Hitesh Aug 19 '14 at 11:52
  • 1
    This worked for me too..Here is how my entityFramework node looks like in config – Pritam Karmakar Aug 23 '14 at 00:21
  • 7
    To be clear: Adding the above code to your `DbContext` class will solve the problem in EF 6.1. This way you don't need to include the Entity Framework Nuget package in your front-end (WebApi, etc) project and can leave everything EF-related in your data layer. – Nick Nov 11 '15 at 21:34
  • 1
    @AndersLindén, You should place the reference in code in your data context/ entities project. See http://stackoverflow.com/a/29743758/52277 – Michael Freidgeim Jun 07 '16 at 14:12
  • I was eceptical about this, but it saved me day, thanks..oh so many thanks – Mauricio Gracia Gutierrez Mar 08 '18 at 20:26
  • Still an issue in VS 2017, EF 6.2. Adding the line mentioned above in the test class will solve the problem, you do not need to polute your DbContext – LilleElefant May 09 '18 at 07:42
  • VS 2017, EF 6.2 here, opted to put this method at the bottom of the DbContext because maintaining "separation of concerns" in our assemblies is worth the bit of extra code. – jaybro Nov 05 '19 at 23:10
111

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.

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;
}
Timothy Schoonover
  • 2,945
  • 3
  • 27
  • 41
  • 1
    This solved the issue for me with respect to MSTest. I like this method because it doesn't require that I do anything messy with class inheritance on my test classes. Just include the class somewhere in the test assembly and MAGIC. Thanks! – kbrimington Feb 09 '15 at 15:21
  • Exactly, this is the reason it is not copying the sqlserver.dll to the referencing projects output. This happens when you have one more layer in between executing application and Context assembly. Thanks this solved my issue. – Bharat Aug 29 '15 at 21:55
  • 2
    For those not wanting to spend 4 seconds looking it up: using System.Data.Entity.SqlServer; – TTT Dec 02 '15 at 21:25
  • We use a common base class for unit tests, all unit tests inherit from this class. Adding this method to that one class means that all projects with a unit test class inheriting from the base class work OK. – MaxJ Dec 10 '15 at 10:52
  • 3
    Small point, but it's probably best to add a comment to this class to explain why it is necessary. – John Darvill Mar 04 '16 at 12:32
  • @JD41 Agreed. Including the 'share' link for this answer with the text "For more info" might be enough, but a detailed explanation of a few sentences would be very polite to future sanity. – Dan Esparza Jul 20 '16 at 18:43
45

My solution was to remove the entity framework from the project via the nuget manager and add it back in.

Kevbo
  • 939
  • 9
  • 11
32

I solved this by adding an using stament on top of my DBContext class, like so:

using SqlProviderServices= System.Data.Entity.SqlServer.SqlProviderServices;
mmttato
  • 533
  • 6
  • 8
  • 1
    This is the cleanest solution to this problem. Thank you. – Alexandru Dicu Apr 19 '16 at 14:06
  • 1
    This solution works and looks clean. Question: wont we loose the line id the DBContext class is refreshed? i.e. using the edmx class generation? – NoloMokgosi May 05 '16 at 10:30
  • Can't believe this was necessary and somehow hasn't been corrected by Microsoft - but this worked for me. Thanks for a simple solution. – Tsar Bomba Sep 22 '16 at 10:25
  • 2
    This solution is functionally the same as [MissingDllHack](http://stackoverflow.com/a/22315164/1141360). It may seem cleaner, but introduces workaround code into your context class. This code will also be flagged for removal by tools like ReSharper and could easily be lost over time. A good solution, but I prefer to keep the workaround separate from the context in a way that does not produce code warnings. – Timothy Schoonover Oct 11 '16 at 14:30
20

I've used Code-based registration for provider. link1 link2

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

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)
    {
        ...
    }
}
vkuschenko
  • 301
  • 2
  • 3
  • I have two projects in one solution. The first is the class-library with the entity framework assembly an the DbContext class and the tt-files. The second project is windows forms that should get data by using the ef-project. @Nash: Could you elaborate where (into which project) you placed your class `DbContextConfiguration`? Thanks – surfmuggle Dec 16 '13 at 10:10
  • As mentioned above it seems that additional references are one way to fix this (see comment from eric). I used the nuget console and fired this command: `Get-Project MyWinformsProject | Install-Package EntityFramework` which fixed it for me. Still i would like to better understand the reason. – surfmuggle Dec 16 '13 at 10:19
  • DbContextConfiguration seemed to already be present (vs 2013), adding the DbConfiguraton line above my MyDbContext class did not solve the issue for me. – Anders Lindén Jun 07 '16 at 12:31
  • You don't need a _DbConfiguration_ class. Put the following code at app startup (before EF is used): `DbConfiguration.Loaded += (sender, e) => e.ReplaceService((s, k) => System.Data.Entity.SqlServer.SqlProviderServices.Instance);`. – Rosberg Linhares Mar 01 '17 at 22:03
14

I sorted it out with [DeploymentItem] on my assembly initializing class

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());
        }
    }
}
Random
  • 3,185
  • 1
  • 32
  • 44
9

Late to the party, but the top voted answers all seemed like hacks to me.

All I did was remove the following from my app.config in the test project. Worked.

  <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>
jeriley
  • 1,253
  • 12
  • 17
8

I have problem, because I don't add reference to EntityFramework.sqlServer.dll. When I develop program, it works. But when I publish app and install it, it throws error.

I just add reference and Build and Publish again.

References

Matej
  • 156
  • 3
  • 8
  • I did this one and its worked. Basically the project will show an warning when the components to the libraries where not referred properly. – kbvishnu Nov 12 '19 at 18:06
5

I have solved this by manually copying EntityFramework.SqlServer.dll file to the bin folder of the main application.

Vinoth Krishnan
  • 2,849
  • 6
  • 25
  • 33
Sreeja Sj
  • 51
  • 1
  • 1
4

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.

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

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.

I discovered the root cause by manually debugging the test ( which I only realised after so many days , the fix took me 5 seconds).

Hopefully this will help someone who comes across such issues.

mybrave
  • 1,285
  • 3
  • 14
  • 32
ashutosh raina
  • 8,700
  • 10
  • 40
  • 80
4

I see similar problem, and using the method from this post: (http://entityframework.codeplex.com/workitem/1590), which solves my problem.

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;

Innovation Wang
  • 199
  • 1
  • 2
  • 6
4

I had same isssue i tried many times but it did not resolve but when i install the package EntityFramework.SqlServerCompact it solved install this package from Nuget package Manager.

Install-Package EntityFramework.SqlServerCompact
Alishan
  • 69
  • 5
3

When I inspected the problem, I have noticed that the following dll were missing in the output folder. The simple solution is copy Entityframework.dll and Entityframework.sqlserver.dll with the app.config to the output folder if the application is on debug mode. At the same time change, the build option parameter "Copy to output folder" of app.config to copy always. This will solve your problem.

3

Simply reference or browser the EF dll - EntityFramework.SqlServer.dll

yogihosting
  • 3,745
  • 2
  • 31
  • 54
3

After trying all other suggested solutions and don't getting my project to work, I finally found a little comment in this page:

Deleting the BIN-Folder did it for me

And it worked for me too.

Ulysses Alves
  • 1,960
  • 1
  • 18
  • 32
  • 1
    I was merging 2 branches - my project had no changes related to EF, but my unittests just started failing for no reason. Deleting the BIN folder got them working again. – Arno Peters Apr 30 '19 at 06:15
  • @Zeek2 this error is a tricky one. There are many different suggestions to solve this issue in the page I’ve quoted. This is just one of many possible solutions. Keep researching it until you find the solution which will apply to your particular issue, and good luck. – Ulysses Alves Jun 07 '19 at 12:38
  • 1
    Really weird that this was my solution too. – Harry Adams Dec 03 '20 at 15:31
3

I've created a static "startup" file and added the code to force the DLL to be copied to the bin folder in it as a way to separate this 'configuration'.


[DbConfigurationType(typeof(DbContextConfiguration))] public static class Startup { }

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

hatsrumandcode
  • 1,821
  • 2
  • 19
  • 19
2

I didn't want a reference to EF in my application project (or to manually copy anything) so I added this to my EF project's Post-build events:

cp $(TargetDir)EntityFramework.SqlServer.dll $(SolutionDir){your-main-bin-folder}
patrickbadley
  • 2,167
  • 1
  • 24
  • 24
2

This only happens inside my load/unit testing projects. Frustrating, I jyst had it crop up in a project I have been running for 2 years. Must have been some order of test running that breaks things. I guess once that fi gets removed its gone.

I found that simply declaring a variable that uses the correct value fixes the issue... I never even call the method. Just define it. Odd but it works.

    /// <summary>
    /// So that the test runner copies dlls not directly referenced by the integration project
    /// </summary>
    private void referenceLibs()
    {
        var useless = SqlProviderServices.Instance;
    }
hal9000
  • 714
  • 8
  • 20
2

I also had a similar problem

My problem was solved by doing the following:

enter image description here

enter image description here

BehrouzMoslem
  • 6,792
  • 3
  • 24
  • 33
1

Adding Entityframework.dll and Entityframework.sqlserver.dll to the reference project solved the issue.

Nish
  • 101
  • 1
  • 6
  • yes, I believe my csproj file was somehow referencing 6.0.0 when my packages.config was pointing to 6.1.3 (due to me moving some furniture around) – bkwdesign Jul 30 '20 at 21:36
1

I checked Debug Output window in Unit Test project. EntityFramework.SqlServer.dll was not loaded. After adding it to the bin folder tests were run successfully.

yW0K5o
  • 844
  • 1
  • 15
  • 29
1

I had the same issue with Instantiating DBContext object from a unit test project. I checked my unit test project packages and I figured that EntityFramework package was not installed, I installed that from Nuget and problem solved (I think it's EF bug).

happy coding

Code_Worm
  • 2,669
  • 2
  • 25
  • 29
0

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.

hope this helps others

pastrami01
  • 311
  • 4
  • 9
0

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"

Robert

0

There is a easy fix. open the references in your project, right click "System.Data" -> properties. Change "Copy Local" to "True".

Problem should be fixed.

ChinaHelloWorld
  • 775
  • 1
  • 8
  • 6
0

In my case I resolved the problem by installing SQL Server 2012 Developer Edition when I had previously installed SQL Server Express 2012 (x64). It seems that provided me with the missing dependency.

Farrukh Najmi
  • 4,418
  • 2
  • 29
  • 45
0

remove the entity framework from the project via nuget then add it back in.

Griffo
  • 63
  • 6
0

In my case dll was not copied although I added a reference to it. This is because EntityFramework.SqlServer.dllis not copied into your project. Add that dll and it will hopefully work.You can find that from the project where you added datamodel.

Baqer Naqvi
  • 4,058
  • 2
  • 37
  • 61
0

Additional to all useful suggestions here, if you are using EF 6.1.3, get sure your project's .net version is 4.5 or more.

Deilan
  • 4,100
  • 3
  • 31
  • 47
Mauricio Atanache
  • 1,574
  • 1
  • 11
  • 17
0

The problem in my case was that in order to catch another exception I had enabled common language runtime (CLR) exceptions. And I forgot to disable it.

I disabled it in my exception setting. and it overlooked this exception and went on to run and create a db for me (in my case) automatically.

Blue Clouds
  • 4,483
  • 2
  • 30
  • 53
0

Make sure EntityFramework.dll, EntityFramework.SqlServer.dll and your provider's dll (for SQL Server Compact, that will EntityFramework.SqlServerCompact.dll) are in the deployed application folder. This is especially important for deployed application.

falopsy
  • 391
  • 4
  • 15
0

I had encountered exactly the same problem on my CI build server (running Bamboo), which doesn't install any Visual Studio IDE on it.

Without making any code changing for the build/test process (which I don't think is good solution), the best way is to copy the EntityFramework.SqlServer.dll and paste it to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE. (where your mstest running)

Problem solved!

Kelvin
  • 81
  • 1
  • 1
  • 5
0

Click on Reference> check EntityFramework Reference is present or not

If not available Add it

Click on

Add Reference > Add entity framework 1)EntityFrameWork 2)EntityFrameWork.SqlServer

Siddhartha
  • 1,073
  • 10
  • 9