[英]Asp.net mvc 4 how to use WebSecurity.createUserAndAccount with custom field
I have a problem with I create a custom field in UserProfile
table.like 我在
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 我还将
UserDetail
添加到DbContext
类
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. 它总是会出现一些错误,例如:对象类型不存在映射......但是如果我定义一个简单类型(如
string
, int
)而不是UserDetail
,它可以正常工作。
Anyone can help me solve this problem? 有人可以帮我解决这个问题吗? Thanks very much!!
非常感谢!!
I had a similar problem to this and got it work by: 我遇到了类似的问题并通过以下方式解决了问题:
combine UserDetail and UserProfile to something along this line: 将UserDetail和UserProfile结合到这一行:
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 更新你的[HttpPost]注册表
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 不要忘记根据需要将新字段添加到RegisterModel中
public class RegisterModel
{
....
public string FirstName{get;set;}
public string LastName{get;set;}
}
hope this works for you 希望这对你有用
I think you are looking to do this. 我想你是想做到这一点的。
UserDetail UserDetail
public class UserDetail
{
//This is property mapping, UserId will be the same as the Membership UserId and UserProfile UserId
[Key]
[ForeignKey("UserProfile")]
[HiddenInput(DisplayValue = false)]
public int UserId { get; set; }
public string FirstName{get;set;}
public string LastName{get;set;}
public UserProfile UserProfile { get; set; }
}
RegisterModel RegisterModel
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; }
[Required]
public string FirstName{get;set;}
[Required]
public string LastName{get;set;}
}
Register Action 注册行动
//
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
var db = new UsersContext();
// Attempt to register the user
try
{
var userProfile = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, false);
//var userProfile= db.UserProfile.SingleOrDefault(u => u.UserName == model.UserName);
if (userProfile!= null) //This way Userdetail is only created if UserProfile exists so that it can retrieve the foreign key
{
var userDetail = new UserDetail
{
UserProfile = userProfile,
FirstName = model.FirstName,
LastName = model.LastName
};
db.UserDetails.Add(userDetail);
db.SaveChanges();
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Edit 编辑
Depending on how you have set up your Context and repositories, you will need something like below for each of your POCO classes 根据您设置Context和存储库的方式,每个POCO类都需要类似下面的内容
public DbSet<UserProfile> UserProfiles { get; set; }
IQueryable<UserProfile> IDataSource.UserProfiles
{
get { return UserProfiles; }
}
In addition to SimpleMembership tables, you will need to add the following to your context for many-to-many
between Membership and Roles
除了SimpleMembership表之外,您还需要在上下文中添加以下内容,以便在
Membership and Roles
之间进行many-to-many
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Membership>()
.HasMany<Role>(r => r.Roles)
.WithMany(u => u.Members)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.