Can SimpleMembership be used with EF model-first? When I try it, I get “Unable to find the requested .NET Framework Data Provider” when I call WebSecurity.InitializeDatabaseConnection.
To put it another way: I can’t get the call to WebSecurity.InitializeDatabaseConnection to work when the connection string employs the System.Data.EntityClient provider (as it does when using the model-first paradigm).
To repro the issue, create an MVC 4 app, and replace the code-first UserProfile entity class (which you get for free with the MVC 4 template) with a model-first User class that you have created in the Entity Designer:
- Create an MVC 4 app in VS 2012 and add a new, blank Entity Data
Model. - Add a new Entity named
Userto the model, with fields forId,
UserName, and FullName. So, at this point, theUserdata entity is
mapped to aUserstable and is accessed via a funky connection
string that employs theSystem.Data.EntityClientprovider. - Verify that the EF can access the
Userentity. One easy way to do
that is to scaffold out a Users controller based on the User table
and its associated DbContext. - Edit the
AccountModels.csfile to remove theUserProfileclass and
its associatedUsersContextclass. Replace the references to the
(now missing)UserProfileandUsersContextclasses with references
to your new User class and its associatedDbContextclass. - Move the call to InitializeDatabaseConnection from the
InitializeSimpleMembershipAttribute filter class to the
Application_Start method in Global.asax.cs. While you’re at it,
modify the arguments to use your new User entity’s connection
string, table name, and UserId column name. - Delete the (no longer used)
InitializeSimpleMembershipAttribute
class and the references to it.
When you run the repro, it will get an Exception at the call to InitializeDatabaseConnection.
Bob
SimpleMembership can work with model first. Here is the solution.
1.
InitializeSimpleMembershipAttribute.csfrom MVC 4 Internet Application templete should look like this2.Delete CodeFirst Classes from
AcountModel.cs3.Fix
AccountCotroler.csto work with your Model-first DbContext (ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)method)4.Define your
"ConnStringForWebSecurity"connection string which is not same as that funky conn string for model-first db access, notice that we use providerSystem.Data.SqlClientnotSystem.Data.EntityClient