I faced with new ASP MVC 4 feature, it shipped with new membership db schema and new initialization. In mvc 3 and old versions developer able to create custom user profile fields using specifications in web.config, but now i faced with method in filters namespace in default mvc 4 project:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
and user profile table:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
But the method InitializeDatabaseConnection generate only UserName and UserId i need to generate other additional fields.
I have good experience in EF codeFirst approach, and in that case i try to edit UserProfile Class:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Column]
[Required]
public string UserName { get; set; }
[Column]
[Required]
public string FirstName { get; set; }
[Column]
[Required]
public string LastName { get; set; }
}
But when i regenerate database, i havent see any changes, Custom Db fields not generated.
Help me please, how can i create custom user fields?
Elaborating from the answer above
The
WebSecurity.InitializeDatabaseConnectionMethod help states thatSo if we want more fields into the
UserProfiletable we just need to make sure we are creating a profile table and run theInitializeDatabaseConnectionmethod after the table is already in place.In the standard MVC4.0 project template from VS2012 I’ve commented out the Account controller
and moved
InitializeDatabaseConnectioninto the EF Code First Database Initializerensuring that the
InitializeDatabaseConnectionruns once the table is already in place.Added the
UserProfileclass to my EF Code First modelAdded the extra field in the
UserProfiletableAll 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.