0

I'm created windows service where I have problem in calling query using entity frame-work inside this service, its always return null.

I tried to reinstall entity-framework package from NuGet but nothing happend.

this is my app.config file part:

<add name="GPSContext" connectionString="data source=server_name;initial catalog=dbName;user id=username;password=****;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 

and (

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.

) run time error returned when I configured my entity-framework:

   <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

<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>
  • Make sure you have the `EntityFramework` and `EntityFramework.SqlServer` references in your project (the calling assembly / runnable project) – Jordy van Eijk Jul 16 '18 at 11:59
  • Yes I checked it and I have "EntityFramework" and "EntityFramework.SqlServer" references in my project – Mohammed Alasa'ad Jul 16 '18 at 12:01
  • You are sure that you did not add EF to a Library project and referencing it from another project? – Jordy van Eijk Jul 16 '18 at 12:03
  • When I added EF I added it from NuGet but when I configure it I copied code from another project you think is this the problem and if yes, how can I configure my EF in app.config file? Thanks for help – Mohammed Alasa'ad Jul 16 '18 at 12:08
  • If I need to use EF in a library I will add it using Nuget if I have a Calling Assembly lets say my WPF app i will also install EF using Nuget there to make sure it is all working correctly. But take a look at my answer it will probably fix your problems :) – Jordy van Eijk Jul 16 '18 at 12:13

1 Answers1

0

You can do a couple of things...

Make sure you have both EntityFramework and EntityFramework.SqlServer added to the references of the calling assembly.

If this will not fix your problem you can delete the BIN folder and try again.

If this still isn't working add the following code somewhere in your calling assembly

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

the compiler sometimes removes the entityFramework.SqlServer.dll if its not referenced! You do not need to do anything with this code!

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37