Asp.net mvc 4 how to use WebSecurity.createUserAndAccount with custom field
asp.net, asp.net-mvc-4, c#
Solution
I had a similar problem to this and got it work by:
combine UserDetail and UserProfile to something along this line:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName{get;set;}
public string LastName{get;set;}
}
update your [HttpPost] Register
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
propertyValues: new { FirstName= model.FirstName, LastName = model.LastName}, false);
don't forget to add the new fields into your RegisterModel as needed
public class RegisterModel
{
....
public string FirstName{get;set;}
public string LastName{get;set;}
}
hope this works for you
Problem
I have a problem with I create a custom field in `UserProfile` table.like ``` public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public int? AddressId { get; set; } public int? UserDetailId { get; set; } public string UserName { get; set; } public UserDetail UserDetail { get; set; } } public class RegisterModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } public virtual UserDetail UserDetail { get; set; } } public class UserDetail { public int Id{get;set;} public string FirstName{get;set;} public string LastName{get;set;} } ``` And I also added `UserDetail` to `DbContext` class ``` public DbSet<UserDetail> UserDetails{get;set;} ``` The Problem is when I use ``` Web WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { UserDetail = new UserDetail () }, false); ``` It always comes up with some error like :No mapping exists from object type... But If I define a simple type (like `string`, `int`) instead of `UserDetail`, it works fine. Anyone can help me solve this problem? Thanks very much!!