Entity Framework Code First Migrations failing on Wrong Provider (3.5 instead of 4.0)

大城市里の小女人 提交于 2020-01-11 07:23:06

问题


I am trying to add a migration (for the first time) to a project using Entity Framework 6.0.2 for SQL Server Compact (code first). The application is having no problems communicating with the database. The migration is failing with the error message:

System.InvalidOperationException:
No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.3.5'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

The project has never had any reference to SqlServerCe.3.5 and I cannot find one with Visual Studio find & replace command

The app.config file has:

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
      </dependentAssembly>
  </assemblyBinding>
</runtime>
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="System.Data.SqlServerCe.4.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>
<appSettings>
<system.data>
  <DbProviderFactories>
    <remove invariant="System.Data.SqlServerCe.4.0" />
    <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  </DbProviderFactories>
</system.data>

Does anyone know how I can force migrations to use SqlServerCe.4.0 (+/- remove the reference to 3.5)? Thank you.

Edit

Many Thanks to Mr Sql Server Compact (ErikEJ) for his suggestions.

unfortunately, removing the binding redirect node just results in:

System.Data.Entity.Core.ProviderIncompatibleException: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct. ---> System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.ArgumentException: The connection given is not of type 'SqlCeConnection'.

You are absolutely correct - this project is using private deployment. Referenced assembly versions for System.Data.SqlServerCe and System.Data.SqlServerCe.Entity are both to 4.0.0.1

Edit 2 - solved

I needed to remove the binding redirect node, as suggested by erik, AND add the provider to the EntityFramework node of the app config (the application uses code-based configuration of the provider services, so these were not included) as below

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>

回答1:


Remove the binding redirect. It looks like you are trying to use private deployement, but are you using the correct System.data.SqlServerCe.dll (check the Assembly version). if it is 4.0.0.0 and not 4.0.0.1, then change to DbProviderfactories setting to point to the correct assembly version




回答2:


I just spent half a day trying to get SqlServerCe 4.0 SP1 aka 4.0.0.1 to work without above exceptions. Finally solved, hope it'd help someone else.

  1. Install SqlServerCe 4.0 SP1 either from here or via web platform installer
  2. Copy "Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Private" amd64 and x86 to bin folder of your web app and include them in project (so that they become deployable)

  3. Do not copy "System.Data.SqlServerCe.dll" and "System.Data.SqlServerCe.Entity.dll"

  4. nuget "Microsoft.SqlServer.Compact" was installed instead of obsolete "SqlServerCompact"
  5. project references do not contain System.Data.SqlServerCe, however it would exist in bin folder after project clean/build procedure
  6. web.config

answers above are valid BUT "Version=4.0.0.1" didn't work for me, instead just "4.0"

<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
</system.data>


来源:https://stackoverflow.com/questions/21875246/entity-framework-code-first-migrations-failing-on-wrong-provider-3-5-instead-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!