locked
Updated to EF6 and now my unit-tests don't work ->'System.Data.SqlClient' could not be loaded RRS feed

  • Question

  • Hi,

    I upraded my application to now use EF6. The project its self works fine, but I have a issue with my unit tests.
    When I run them with MS Test (VS2013), there are a few (13) unit test that fail with the exception shown below. If I then run the failed tests again they pass.

    When I run them with ReSharper the all pass.

    Is there something I am doing wrong?

    Regards
    Rainer

    Exception:

    System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
    TestCleanup method Qutronic.BfpNetR2.Testing.UnitTests.SystemRepositoryTesting.With_SystemRepository.MyTestCleanup threw exception. System.InvalidOperationException: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information..

    • Moved by Amanda Zhu Monday, December 2, 2013 6:17 AM More related
    Sunday, December 1, 2013 9:47 AM

Answers

  • Hi,

    Not sure if you already found your solution. I also encounter this issue and able to solve it by adding the following line in my source code..

    public class MyDbContext : DbContext
    {      

            static MyDbContext ()
            {
                var _ = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
                var __ = typeof(System.Data.Entity.SqlServerCompact.SqlCeProviderServices);
            }

    .....

    }

    Cheer :)

    • Marked as answer by Rainer Queck Tuesday, December 10, 2013 8:08 AM
    Monday, December 9, 2013 8:07 AM

All replies

  • Hi,

    Because the error is more related to Entiry Framework rather than unit test itself, I am moving this thread to ADO.NET Entity Framework and LINQ to Entities forum for better solutions.

    Thanks


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 2, 2013 6:17 AM
  • Hello,

    As far as I know, the Entity Framework is now being developed under an open-source license and EF6 and above will not be shipped as part of the .NET Framework. This has many advantages but also requires that EF providers be rebuilt against the EF6 assemblies. This means that EF providers for EF5 and below will not work with EF6 until they have been rebuilt.

    For registering EF providers, we can use either code-based configuration or in the application’s config file.

    Registration of the EF provider in app.config or web.config has the following format:

    <entityFramework>
    
       <providers>
    
         <provider invariantName="My.Invariant.Name" type="MyProvider.MyProviderServices, MyAssembly" />
    
       </providers>
    
    </entityFramework>
    

    The normal way to register an EF provider using code-based configuration is to create a new class that derives from System.Data.Entity.DbConfgiuration and place it in the same assembly as the DbContext class. The DbConfiguration class should then register the provider in its constructor. For example, to register the SQL Compact provider the DbConfiguration class looks like this:

    public class MyConfiguration : DbConfiguration
    
    {
    
        public MyConfiguration()
    
        {
    
            SetProviderServices(
    
                SqlCeProviderServices.ProviderInvariantName,
    
                SqlCeProviderServices.Instance);
    
        }
    
    }
    

    In this code “SqlCeProviderServices.ProviderInvariantName” is a convenience for the SQL Server Compact provider invariant name string (“System.Data.SqlServerCe.4.0”) and SqlCeProviderServices.Instance returns the singleton instance of the SQL Compact EF provider.

    For Details, see it here.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Monday, December 2, 2013 8:25 AM
    Monday, December 2, 2013 8:25 AM
  • Hi Amanda,

    to me actually this issue is more unit-test related. My "production code" is running fine! Its just the unit-test that fail and this only if I run them "ALL". If I then run just the faulted test they turn green.

    Regards
    Rainer

    Tuesday, December 3, 2013 9:51 AM
  • Hello Fred,

    thanks for answering to my thread. I already have been on page you pointed. And yes I still have some TODOs on that subject. But before I start to work on that edge I would like to know, why my tests fail and this only if executed in the "run all" mode, while if executing only all failed test they turn green...

    Regards
    Rainer

    Tuesday, December 3, 2013 9:54 AM
  • Hi,

    "Because the error is more related to Entiry Framework rather than unit test itself"

    I meant that your error message is more related to EF. I am not Entity Frameowrk Expert. Moreover you said that the failed unit test can run successfully, so I think that your unit test should be fine and maybe something related to EF affects unit test run.

    "this only if I run them "ALL". If I then run just the faulted test they turn green."

    Please run every unit test alone instead of running them 'All' to check whether the unit test will fail with the error posted in your original post.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Amanda Zhu Tuesday, December 3, 2013 10:05 AM edit
    Tuesday, December 3, 2013 10:02 AM
  • Please make sure, that EF-related dlls in bin folder of your test project are the recent ones (you may copy them from the bin folder of the working project). I've faced the same issue recently after EF update: new EntityFramework.SqlServer.dll was not copied to the output folder during build. 
    • Proposed as answer by NFolsom Wednesday, June 25, 2014 3:08 PM
    Thursday, December 5, 2013 10:45 AM
  • Hi Exial,

    this is given, otherwise the test wouldn't run at all, right?

    Regards
    Rainer

    Saturday, December 7, 2013 10:49 AM
  • Hi Amanda,

    you might be right, I don't know....

    What confuses me, is that the app runs without problems and the failing tests on the second try run fine true...

    Regards
    Rainer

    Saturday, December 7, 2013 10:53 AM
  • I am seeing the exact thing as Rainer. It is extremely frustrating to say the least. Upgraded from a fully functional EF5 solution to EF6. Application appears to run just fine after upgrade. When running all unit tests via ReSharper test runner, all tests pass successfully. BUT...running all tests via MS Test causes a large number of tests to fail with the same error message:

    Result Message: Unable to create instance of class XXXXXX.Domain.Test.Unit.DecisionSupport.DecisionSupportServiceTests. Error: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

    If I then attempt to rerun those failed tests one at a time, they pass. But if I try to run them all at once (as a build server would do), they always fail. 

    All tests used to pass in both ReSharper and MS Test prior to the EF 6.0.1 upgrade. This is a huge pain and I have wasted a lot of time trying to track down the cause.

    Please escalate this issue. I need a fix!



    Monday, December 9, 2013 2:10 AM
  • Hi Rainer, Eric,

    Since you encountered the same issue, in my opinion, I doubt maybe it is really an issue on Entity Framework upgrade + unit test. You could submit this feedback to Microsoft Connect feedback portal: http://connect.microsoft.com,
    Microsoft engineers will evaluate them seriously. Thanks for your understanding. After you submit the feedback, you can post the link here which will be beneficial for other members with the similar issue. And we will help you to vote it.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Amanda Zhu Monday, December 9, 2013 6:20 AM edit
    Monday, December 9, 2013 6:19 AM
  • Hi,

    Not sure if you already found your solution. I also encounter this issue and able to solve it by adding the following line in my source code..

    public class MyDbContext : DbContext
    {      

            static MyDbContext ()
            {
                var _ = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
                var __ = typeof(System.Data.Entity.SqlServerCompact.SqlCeProviderServices);
            }

    .....

    }

    Cheer :)

    • Marked as answer by Rainer Queck Tuesday, December 10, 2013 8:08 AM
    Monday, December 9, 2013 8:07 AM
  • Oddly enough, adding the first line of the proposed static constructor to a TestInitialization class seems to have corrected the issue. Thanks, Kevan!

    The bigger question is WHY? I shouldn't have to add a seemingly do-nothing line of code to a testing solution in order for it to work in MS Test. Either there is something else I am missing around the test project or test runner configuration OR EF 6 still has some issues where testing is concerned. Which is it?

    Can anyone explain why the proposed workaround works?


    Monday, December 9, 2013 3:46 PM
  • Amanda - I'm not sure I understand your point here. How is this not an issue involving the combination of EF6 and MS Test? That's exactly what it is?
    Monday, December 9, 2013 3:48 PM
  • Rainer - Are you using any Entity Framework testing or mocking frameworks when you run your unit tests? Like Effort or something like that?
    Monday, December 9, 2013 3:49 PM
  • Hi Kevan,

    thank you very much for this hint!!
    It solved my issue.

    I do very much agree with Eric, this solution seems to be very Oddly.
    I would really like to have a explanation for this

    Regards
    Rainer

    Tuesday, December 10, 2013 8:08 AM
  • Hi Eric,

    for me it was very helpful to know, that I am not the only one to experience this issue!

    I am not sure if I know what you mean by "Entity Framework testing".

    I do have Unit-Test which test my Repositories, using EF.
    I also am but I am using Rhino Mocks for mocking.

    Am am very glad to have Kevans workaround, although I would like to know why that is necessary when using EF6.x

    Regards
    Rainer

    Tuesday, December 10, 2013 8:13 AM
  • Kevan - 

    What is meant was: are you testing your repositories against a physical database via the actual EF components, mocking the Entity Framework components used by your repositories, or using an in-memory database solution like Effort?

    Tuesday, December 10, 2013 3:19 PM
  • HI Eric,

    I suppose you ment me, Rainer ;-)

    I am testing my repositories against a physical databese (sdf -> SqlServerCompact).

    This is, why I am using repositories, which are "Interfaced" so that I can mock them in the other unit tests.

    Regards
    Rainer

    Wednesday, December 11, 2013 9:50 AM
  • I faced the similar Issue .. and there is very funny solution for that .. Just look into your ConnectionString -- If it is same that you have used for your other ASP.Net apps, then it should not be... Entity Framework has a different case altogether

    <add name="EmployeeContext" connectionString="Server=.\SQLEXPRESS;Database=DB1;User Id=user1;password=password1;" providerName="System.Data.SqlClient;" />

    Change it to -- 

    <add name="EmployeeContext" connectionString="Server=.\SQLEXPRESS;Database=DB1;User Id=user1;password=password1;" providerName="System.Data.SqlClient"/>

    Can you identify the difference .. only one small -- There is no ';' at the end of ProviderName ..
    Yes and that makes the difference .. it should be same to same copy of the ProviderName, though I have not checked the case sensitivity. But that solved my Problem 
    Friday, December 20, 2013 10:58 AM
  • I'm not sure how this suggestion is relevant to the issue/question at hand. What you have provided is not an Entity Framework connection string. Am I missing something?
    Friday, December 20, 2013 2:30 PM
  • Just wanted to add my experiences here too. To me this strange bug applied after adding a data driven test. Its just:

    [DataSource(@"Microsoft.VisualStudio.TestTools.DataSource.CSV", "DateTimeExtensionsTest_BeginOfDay.csv", "DateTimeExtensionsTest_BeginOfDay#csv", DataAccessMethod.Sequential)]
    public void BeginOfDayTest() ....

    As soon as I have this test in my project (no changes to config at all!!!) the "Run All" command fails. When I comment it out like this:

    /// <summary>
    /// Tests the correctness of the extension-method <see cref="DateTimeExtensions.BeginOfDay"/>.
    /// </summary>        
    [TestMethod]
    //[DeploymentItem(@"Data Sources\DateTimeExtensionsTest_BeginOfDay.csv")]
    //[DataSource(@"Microsoft.VisualStudio.TestTools.DataSource.CSV", "DateTimeExtensionsTest_BeginOfDay.csv", "DateTimeExtensionsTest_BeginOfDay#csv", DataAccessMethod.Sequential)]
    public void BeginOfDayTest()
    {

    everything work. So don't conentrate on configuration too much!

    EDIT: I know have tried some time: Comes out, that EntityFramework.SqlServer.dll is not copied to the build directory of the testing framework. Strange enough that this is

    • A) Solved by "var x = typeof(System.Data.Entity.SqlServer.SqlProviderServices)"
    • B) Only occurs when I use data driven.

    So my solution is to implement a method in the tests:

    [TestInitialize]
    public void Init()
    {
        var x = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
    }

    I suggest to put this into an abstract base class and let all your unit test derive from it.

    sprinter


    Saturday, February 8, 2014 12:59 PM
  • Thanks. this solved the problem. 

    just to note that you can put the code in a [AssemblyInitialize] method and forget about this.

    Wednesday, February 12, 2014 8:27 AM

  • http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded

    Thursday, February 20, 2014 7:33 AM
  • Hi,

    I work with VS 2012 and have the same problem.

    I have found out that:
    -if I run the tests separately, the working directory is 'Test Project/bin/Debug'.
    -when I choose 'Run All' (or run an OrderedTest), the working directory is 'TestResults'.

    VS doesn't copies all my dlls in the TestResult directory. If I copy the dlls from the Debug directory it works fine.

    Friday, February 21, 2014 10:58 AM
  • Yes, I used the following and now my tests (which run on a build server) now run. Note I'm using mySql provider so you might need to change yours

    [TestClass]
        public class InitializeTestAssembly
        {
            [AssemblyInitialize]
            public static void Init(TestContext context)
            {
                var x = typeof (MySql.Data.MySqlClient.MySqlProviderServices);
            }
        }


    Thursday, March 27, 2014 12:02 PM
  • I have the same issue and dont want to spend days screwing around with some worthless to solve problem.

    is there any way to roll this back?  I dont see the original listed in the options for installing.

    Thursday, April 17, 2014 7:41 PM
  • Thanks. You solved the problem I was having after updating to VS 2013 and EF 6.0. Indeed the EntityFramework.SqlServer.dll is not being copied to (in my case) the service bin folder.

    Update: In fact the real problem was that somehow, maybe in the update to VS 2013 or EF 6, the references to the EF were gone. I readded them and all is well.

    • Edited by NFolsom Wednesday, June 25, 2014 3:19 PM
    Wednesday, June 25, 2014 3:09 PM
  • Dear all,

    Tried the typeof solution, doesn't work for me, the EntityFramework.SqlServer.dll just not there in the test folder.

    I truly believe this is between EF6 and MSTest, especially the [DeploymentItem] attribute in that this thing will copy file over to the test folder and somehow our beloved dll got lost (either not copied at all from the bin, or get override?!)

    Then I come up with this solution, and much simpler

        [DeploymentItem(@"EntityFramework.SqlServer.dll")]
    

    Hope this will help you too. :)

    Best,

    Dong

    Friday, July 11, 2014 2:14 PM
  • Hi,

    Not sure if you already found your solution. I also encounter this issue and able to solve it by adding the following line in my source code..

    public class MyDbContext : DbContext
    {      

            static MyDbContext ()
            {
                var _ = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
                var __ = typeof(System.Data.Entity.SqlServerCompact.SqlCeProviderServices);
            }

    .....

    }

    Cheer :)

    Thanks for the solution this fixed it for me too.  Like the others I'm puzzled why this is needed in EF6 when not EF5.  My only guess would be this is an oddity introduced with with the EF6 open sourcing of providers.
    Sunday, August 10, 2014 1:39 PM
  • I had the same problem and this works fine
    Friday, September 19, 2014 11:21 AM
  • I think the reason why this worked is stated here:

    https://entityframework.codeplex.com/workitem/2352

    "The issue occurs because there is no code in the application that explicitly references types in EntityFramework.SqlServer.dll (or relevant provider assembly) since EF will load the assembly at runtime based on info in the App/Web.config file."

    So that means there is no SqlServer.dll and hence can't be loaded.

    Monday, November 17, 2014 3:47 AM
  • The problem is in the application debug mode, the relevant dlls are not loaded and the package.config may not set be to the correct EF6 dlls. For a solution what you could do copy EntityFramework.dll and EntityFramework.SqlServer.dll along with app.config to the output folder. The app.config build parameter "Copy to output folder" should be set to copy always. I think this will solve your problem.

    Thanks

    TJ

    Wednesday, January 21, 2015 5:04 AM
  • It works! Thanks very much~
    Monday, April 24, 2017 2:28 PM