Elaborating from the answer above
The WebSecurity.InitializeDatabaseConnection
Method help states that
If you want to use a database table that contains user profile information (user names, email addresses, and so on), you specify a connection string and table name that the membership system uses to connect to that information. If you do not want to use an existing user profile table, you can specify that the InitializeDatabaseConnection() method should automatically create the user profile table. (A database for the user profile table must already exist.)
So if we want more fields into the UserProfile
table we just need to make sure we are creating a profile table and run the InitializeDatabaseConnection
method after the table is already in place.
In the standard MVC4.0 project template from VS2012 I’ve commented out the Account controller
[Authorize]
//[InitializeSimpleMembership]
public class AccountController : Controller
{
and moved InitializeDatabaseConnection
into the EF Code First Database Initializer
public class MyDatabaseInit: DropCreateDatabaseAlways<MyDatabaseContext>
{
protected override void Seed(MyDatabaseContext context)
{
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("MyDatabaseContext",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}
ensuring that the InitializeDatabaseConnection
runs once the table is already in place.
Added the UserProfile
class to my EF Code First model
public class MyDatabaseContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Added the extra field in the UserProfile
table
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string MobilePhone { get; set; }
}
All you need now is to set the database initialization strategy when the application starts and also call a query on the database the make sure it gets created at that point, before any authorization/authentication code is called.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
Database.SetInitializer<MyDatabaseContext>(new MyDatabaseInit());
new MyDatabaseContext().UserProfile.Find(1);
}