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