我有一个在我的bd中创建用户的问题。 我使用userRepository在aspnet架构中创建用户。
因此,对于创建用户,我必须在aspnet_user和aspnet_membership表中创建一行
userId是一个身份密钥,所以我的bdd管理这个...所以现在,我的UserRepository类中有这个代码:
public aspnet_User CreateUser(string username, string password, string email)
{
using (WebsiteEntities db = new WebsiteEntities())
{
aspnet_User user = new aspnet_User();
user.UserName = username;
user.Email = email;
db.aspnet_User.Add(user);
db.SaveChanges();
**User u = GetUser(username);**
Aspnet_Membership member = new Aspnet_Membership();
member.UserId = u.UserId;
member.PasswordSalt = CreateSalt();
member.Password = CreatePasswordHash(password, user.PasswordSalt);
member.CreateDate = DateTime.Now;
member.IsApproved = false;
member.IsLockedOut = false;
member.LastLockoutDate = DateTime.Now;
member.LastLoginDate = DateTime.Now;
db.aspnet_Membership.Add(member);
db.SaveChanges();
return u;
}
}
但是,我不认为这是在aspnet_User和aspnet_Membership之间创建具有外键的用户的好方法......在这里,我有一个唯一的用户名,但如果不是我不知道我将如何做...
有人可以帮帮我吗?谢谢你的帮助答案 0 :(得分:0)
您应该通过Membership Framework添加新成员。完成后,将UserId用作外键。
这就是我所做的,它可能并不理想,但它对我有用:
var createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == System.Web.Security.MembershipCreateStatus.Success)
{
var newUser = new User();
newUser.DisplayName = model.DisplayName;
newUser.Email = model.Email;
newUser.UserUnits.Add(new UserUnit { UnitId = model.UnitId, Status = (int)UserUnitStatus.Request });
using(var db = new Entities())
{
var temp = db.aspnet_Membership.Single(a => a.LoweredEmail == newUser.Email.ToLower());
newUser.GUID = temp.UserId;
db.Users.AddObject(newUser);
try
{
db.SubmitChanges()
}
catch
{
MembershipService.DeleteUser(model.UserName);
throw;
}
return RedirectToAction("Welcome");
}