0
votes

Consider that EntityFramework 6.13 and Npgsql for EntityFramework6 are installed Consider these are the packages.config and App.config for a windows service. When trying to access the database I get following exception:

InnerException = {"The system cannot find the file specified"}

The connection attempt to the database context throws an exception "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server"

packages.config:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="EntityFramework" version="6.1.3" targetFramework="net45" /> <package id="EntityFramework6.Npgsql" version="3.0.7" targetFramework="net45" /> <package id="Npgsql" version="3.0.7" targetFramework="net45" /> </packages>

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 -->
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql"></provider>
        </providers>
        <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql.EntityFramework" />
      </entityFramework>
      <system.data>
        <DbProviderFactories>
          <remove invariant="Npgsql" />
          <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
        </DbProviderFactories>
      </system.data>
      <connectionStrings>
        <add name="KutaisiDatabase" connectionString="Server=localhost;Database=KutaisiDatabase;User Id=postgres;Password=12345;" providerName="Npgsql" />
      </connectionStrings>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.7.0" newVersion="3.0.7.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>

The most curios fact for me is that the same exact settings work for the windows forms app (not service). What may be the reason for such behavior? Any help will be highly appreciated.

1
I've just found that for the windows service, it is trying to open \\127.0.0.1\PIPE\sql\query - Kaissa
The answer is FOUND: The reference project did not copy the application.exe.config to the output directory, so the service was treating the connection string like the SQL server connection string, thus for the postgres database it did not work. - Kaissa

1 Answers

0
votes

The answer is FOUND: The referenced project did not copy the application.exe.config to the output directory, so the service was treating the connection string like the SQL server connection string, thus for the postgres database it did not work.