Problems with ASP.Net Membership and Entity Framework Migrations using .NET 4.5

.net, asp.net, entity-framework-4, membership, visual-studio-2012

Solution

The answer lies on the surface. Take a look at the MSDN page regarding this method: WebSecurity.CreateAccount Method

It presumes that matching user information (represented by userName) already exists in the user profile table. (To create a new user in both the membership table and in the user profile table, use the CreateUserAndAccount(String, String, Object, Boolean) method.)

Problem

I am learning the migrations feature available in Entity Framework and I seem to hit a problem. When using an internet application project template and enabled entity framework migrations etc. I am trying to just create a default user onto the database - using an automatic migration: ``` protected override void Seed(eManager.Web.Infrastructure.DepartmentDb context) { WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "Email", false); if (!WebSecurity.UserExists("ahicks2")) { WebSecurity.CreateAccount("ahicks2", "password", requireConfirmationToken:true); } if (!Roles.RoleExists("Admin")) { Roles.CreateRole("Admin"); } } ``` When I run "Update-Database -verbose" in the package manager I get the following error: Running Seed method. System.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error. at WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName, String password, Boolean requireConfirmationToken) at WebMatrix.WebData.WebSecurity.CreateAccount(String userName, String password, Boolean requireConfirmationToken) The next line in the stack trace is the `Configuration.Seed` call. In my web config I am using the following setting for the membership provider to guarantee the tables are created in the database I want: ``` <membership defaultProvider="DefaultMembershipProvider"> <providers> <clear/> <add name="DefaultMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/> ``` The database tables are being created correctly, but the code falls over on the create user. Can anyone help? I'm sure its something simple. I wanted to test out these features - of course a simple answer is to revert back to using membership providers from asp.net 2.0! but I wanted to try and get this to work. Any help would be appreciated. EDIT: By the way I've tried this: http://forums.asp.net/t/1598261.aspx/1 and it doesn't solve this problem.

Original source