MVC4 - Membership - SQL Server Compact 4.0 - Exception: Unable to find the requested .Net Framework Data Provider

被刻印的时光 ゝ 提交于 2020-02-01 09:40:28

问题


The InitializeDatabaseConnection Method documentation says we can use the MVC 4 "Internet" template's generated membership functionality with SQLCE (SQL Server Compact 4.0 Local Database).

I've used this template many times before and have never had a problem when I didn't fiddle with anything and simply used the localDb (the default) that is generated by the Entity Framework the first time we register a user.

My requirement is to get the membership tables that are by default generated into 2012 localDb database instead to generate into a SQLCE database. However, I'm getting the following:

Exception: Unable to find the requested .Net Framework Data Provider. It may not be installed.

To do this we simply:

  1. Open Visual Studio (express works fine).
  2. Generate a new MVC4 --> Internet (with account) project.
  3. Add a SQLCE database to the ~/App_Data/ folder (right-click the folder and select Add --> SQL Server Compact 4.0 Local Database).
  4. Add table then a record to the table.
  5. Right-click the Models folder and select Add --> ADO.NET Entity Data Model
  6. Open the (root) web.config and copy the name of the 'connectionstring'(<add name="ceEntities")
  7. Locate the following line of code in the ~/Filters/InitializeSimpleMembershipAttribute class: WebSecurity.InitializeDatabaseConnection("ceEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true);
  8. F5 to compile/run/debug the project
  9. Once the home/index page loads click the "Register" link in the upper right hand corner
  10. Enter a username and password to register and click 'Ok.'

Up to this point the code compiles and runs fine however when the following line of code is run in the ~/Filters/InitializeSimpleMembershipAttribute class:

WebSecurity.InitializeDatabaseConnection("ceEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true);

This exception is caught: Unable to find the requested .Net Framework Data Provider. It may not be installed.

Which is apparently fixed when we add this code to the web.config:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <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.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>

And when we run it again the following exception is caught:

Unable to find the requested .Net Framework Data Provider. It may not be installed.


回答1:


Thank you to Scott Hanselman and whoever made him aware of the Membership Provider with SQLCE protocol as in his Introducing System.Web.Providers - ASP.NET Universal Providers for Session, Membership, Roles and User Profile on SQL Compact and SQL Azure blog post Scott outlines the steps to implement the ASP.NET Universal Providers. This helped me as he outlined the proper connection string outline for SQLCE:

<connectionstrings>
   <add name="Sql_CE" 
        connectionstring="Data Source=|DataDirectory|\MyWebSite.sdf;" 
        providername="System.Data.SqlServerCe.4.0">
   </add>
</connectionstrings>

So I updated my connectionstring to:

<connectionstrings>
   <add name="defaultconnection" 
        connectionstring="Data Source=|DataDirectory|\ce.sdf;" 
        providername="System.Data.SqlServerCe.4.0">
   </add>
</connectionstrings>

Then updated connectionStringName the ~/Filters/InitializeSimpleMembershipAttribute class back to:

WebSecurity.InitializeDatabaseConnection("defaultconnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

The application compiled without an error or exception so I ran the application then Registered a user and in that process the applicable SimpleMembership tables were generated into my "ce.sdf" (SQL Server Compact 4.0 Local Database).

I am now able to utilize the SimpleMembership default's at their fullest!

p.s., For brevity I'm going to now edit my initial post/question to omit the stacktraces, leaving only the exceptions.




回答2:


Random suggestion but it may need to be added.as a namespace to the web.config file or as a package in the packages file, I could be wrong



来源:https://stackoverflow.com/questions/16259581/mvc4-membership-sql-server-compact-4-0-exception-unable-to-find-the-reque

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