ErikEJ / EntityFramework6PowerTools

This is the codebase for Entity Framework 6 Power Tools Community Edition, and modern EF 6 providers for SQL Server and SQL Server Compact
Other
183 stars 27 forks source link

Can not use this from app.config configuration only. Documentation bug? #85

Closed ionmincu closed 3 years ago

ionmincu commented 3 years ago

Description

Adding the section as described in documentation does not work.

  <entityFramework>
    <providers>
      <provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, ErikEJ.EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

I've also added:

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

 And as described (https://github.com/dotnet/SqlClient/blob/main/porting-cheat-sheet.md#configuration)

    <system.data>
        <DbProviderFactories>
            <add name="SqlClient Data Provider"
                invariant="Microsoft.Data.SqlClient"
                description=".Net Framework Data Provider for SqlServer"
                type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient" />
        </DbProviderFactories>
    </system.data>

Exception

At startup I'm getting the following exception

System.NotSupportedException: 'Unable to determine the provider name for provider factory of type 'Microsoft.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.'
ErikEJ commented 3 years ago

??? No idea what you are referring to?

ionmincu commented 3 years ago

??? No idea what you are referring to?

Sorry pressed shift enter by mistake and the comment just posted, check edited version 😄

ErikEJ commented 3 years ago

.net core or .net framework?

It works with the Smoke test for .net Framework.

ionmincu commented 3 years ago

.net core or .net framework?

It works with the Smoke test for .net Framework.

.NET Core 5.0

ErikEJ commented 3 years ago

Please share a full repro.

ErikEJ commented 3 years ago

I doubt that will work, but please share a repro.

ionmincu commented 3 years ago

Check, https://github.com/ionmincu/EfTesting PS I gave you admin rights on this repo you should be able to commit to it.

ErikEJ commented 3 years ago

I don't think .NET 5 knows anything about app.config

ionmincu commented 3 years ago

I don't think .NET 5 knows anything about app.config

It most definitely does, try to delete it and you will see another error. EDIT: and we do use app.config in our application

ErikEJ commented 3 years ago

Thanks or the repro, proposed update to the docs:

You can also use XML/App.Config based configuration:

<?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" />
    </configSections>
    <entityFramework>
        <providers>     
            <provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, ErikEJ.EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
</configuration>

If you use App.Config with a .NET Core / .NET 5 or later app, you must register the DbProviderFactory in code once:

DbProviderFactories.RegisterFactory(MicrosoftSqlProviderServices.ProviderInvariantName, Microsoft.Data.SqlClient.SqlClientFactory.Instance);
ionmincu commented 3 years ago

Weeel I kinda figured this out, wanted a config only solution, but I guess it's not really possible (at least not without a small update). Ok fine by me, you can close this if you want, turns out I cant really switch between SDS or MDS because we have some SqlParameter types and they are either SDS or MDS.

ErikEJ commented 3 years ago

Correct, you can only do that with conditional compilation, not at runtime.