SimpleMembership with custom database schema in ASP.NET MVC 4 SimpleMembership with custom database schema in ASP.NET MVC 4 asp.net asp.net

SimpleMembership with custom database schema in ASP.NET MVC 4


I asked the same question to the product team.

The design goal of SIMPLE membership was to work out-of-the box as simple as possible.

So really there's no customization possible as far as the tables are concerned.The recommended workaround is, to still use ASP.NET Membership (SqlMembershipProvider).


Customization is possible.

You can get the source code for SimpleMembershipProvider from the aspnetwebstack project on CodePlex, since this is where the mapping occurs between the DB Schema and the runtime environment you can modify this code to modify/replace schema, statements, etc to match your needs (without much headache, IMO.)


1 - You need to enable migrations, prefereably with EntityFramework 5. Use Enable-Migrations in the NuGet package manager.

2 - Move your

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true); 

to your Seed method in your YourMvcApp/Migrations/Configuration.cs class

    protected override void Seed(UsersContext context)    {        WebSecurity.InitializeDatabaseConnection(            "DefaultConnection",            "UserProfile",            "UserId",            "UserName", autoCreateTables: true);        if (!Roles.RoleExists("Administrator"))            Roles.CreateRole("Administrator");        if (!WebSecurity.UserExists("lelong37"))            WebSecurity.CreateUserAndAccount(                "lelong37",                "password",                new {Mobile = "+19725000000", IsSmsVerified = false});        if (!Roles.GetRolesForUser("lelong37").Contains("Administrator"))            Roles.AddUsersToRoles(new[] {"lelong37"}, new[] {"Administrator"});    }

Now EF5 will be in charge of creating your UserProfile table, after doing so you will call the WebSecurity.InitializeDatabaseConnection to only register SimpleMembershipProvider with the already created UserProfile table, also tellling SimpleMembershipProvider which column is the UserId and UserName. I am also showing an example of how you can add Users, Roles and associating the two in your Seed method with custom UserProfile properties/fields e.g. a user's Mobile (number).

3 - Now when you run update-database from Package Manager Console, EF5 will provision your table with all your custom properties

For additional references please refer to this article with sourcecode: http://blog.longle.io/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/