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

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.

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.

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

Related

Unable to find the requested .Net Framework Data Provider.

I've seen several posts about this, but none of the answers seem to work.
I'm trying to set up simplemembership and I keep getting this error when WebSecurity.InitializeDatabaseConnection(...) is called
Here is the ConnectionsStrings entry:
<add name="JBarterMain" connectionString="Data Source=winsrv01;Initial Catalog=ProvausioJBarter;Persist Security Info=True;User ID=xxx;Password=xxx" providerName="System.Data.SqlClient"/>
I tried adding the following to my machine config in the .net 4.0 x64 folder:
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
I'm getting absolutely nowhere. This seems incredibly odd to me that this wouldn't work out of the box. I had another project a few months ago (broken currently so I can't verify that it still works) that was working just fine and I didn't have to do any additional configuration.
Any ideas?
I wrote a tutorial on how to set up simple membership Here. Also I prefer to ditch the filters and initialize it directly on application start

ASP.NET MVC4 Simple Membership fails to initialize if using Entity Connection String (works with SQL connection string)

I am developing ASP.NET MVC4 application with Entity Framework 5, and I am using Model First mode.
However Simple Membership will throw the following error if I am using the connection string generated by the designer:
......
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
Source=STD
StackTrace:
at SistemTempahanDewan.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\Users\Orang\Documents\Visual Studio 2012\Projects\STD\STD\Filters\InitializeSimpleMembershipAttribute.cs:line 45
InnerException: System.ArgumentException
HResult=-2147024809
Message=Unable to find the requested .Net Framework Data Provider. It may not be installed.
Source=System.Data
StackTrace:
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString)
....
Connection string generated by the designer (will not work):
<add name="STD" connectionString="metadata=res://*/Models.STD.csdl|res://*/Models.STD.ssdl|res://*/Models.STD.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=STD;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Connection string I add manually (will work):
<add name="STD" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=STD;Integrated Security=True;MultipleActiveResultSets=True" />
Yes, SimpleMembership only works with SQL Server connection strings, so you need such an entry in your web.config. Not ideal, but it works as you say...
This may be your answer: Using SimpleMembership with EF model-first
Jon Galloway's article is an excellent resource of information about SimpleMembership, if you haven't already seen it: http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

Connecting to an existing database in a ASP.NET MVC4 Web API project

I'm building an web api using ASP.NET web api framework. I have seen a few tutorials on how to get started but I haven't found to use an existing SQL Server database. I have the database up and running also added the data connection to the database explorer in VS but I don't know how to connect the database to my project so I can start using it as my repository. How can I do that?
Put the following in your application web.config file and replace "yourConnectionString" with the actual connection string.
<configuration>
<connectionStrings>
<add name="DbConnection" connectionString="yourConnectionString" />
</connectionStrings>
</configuration>
In the code use System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString to get its value. You might need to reference the "System.Configuration" assembly in your project.

SecurityException after adding ELMAH to WCF project

I'm having a problem with a WCF (.Net 3.5) site using ELMAH on IIS 7.5. This is the exception message:
System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
To verify that it was the combination of WCF and ELMAH on IIS7.5, I created a new WCF Service project in VS2008 and a new Application under the IIS 7.5 site. I published it and made sure it was okay. Then I added the ELMAH DLL. Still okay. Then added the ELMAH configSection references to Web.config. Still okay. Then I added the ELMAH stuff to httpHandlers and httpModules in system.web (yeah, I know this is not for 7.5). Still okay. Then I added this to handlers and modules in system.webServer:
<handlers>
:
<add name="Elmah" verb="POST,GET,HEAD"
path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
<modules>
:
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
And then I got the security exception. I've made no other config changes.
Everything works using the VS dev webserver. The development machine is 2008 R2 x64 with VS 2008 SP1 installed. The WCF service is built for x86 (because the original project was). ELMAH is the .Net 3.5 x86 version. The IIS7.5 app pool is set to allow 32-bit.
Any idea what I need to do to get this working?
In my case, I had to set the "load User Profile=True" in app pool to make it work.
Steps:
From application pools list, Go to Advanced Settings, Process Model section and set "Load User Profile" to "True".
You will have to go to Add managed handler in the IIS Manager for your virtual web directory.
Request Path will be : elmah.axd
Type will be: Elmah.ErrorLogPageFactory, Elmah
Name: Anything you want it to be identified with.
Note: This settings will be removed every time you publish changes from Visual Studio. So you will have to add it again.

How do I get the .NET Data Provider for SqlServer counter to show up in Perfmon

Has anybody managed to get the ".NET Data Provider for SqlServer" to actually work within perfmon.exe.
I have a .NET app that uses nhibernate to interact with sql server 2005 db. All I want to do is to view the NumberOfActiveConnectionPools, NumberOfActiveConnections and the NumberOfFreeConnections within perfmon.exe
How exactly I get this to work?
There is an app.config setting to enable these counters. Via MSDN:
<system.diagnostics>
<switches>
<add name="ConnectionPoolPerformanceCounterDetail"
value="4"/>
</switches>
</system.diagnostics>