我正在尝试扩展 Simple Membership 以包括 FirstName、SecondName 和电子邮件地址,但是 CreateUserAndAccount 出现了一些问题。
所以基本上在RegisterModel中我添加了以下内容: -
[Display(Name = "First Name")]
[Required(ErrorMessage = "Text is required")]
public string FirstName { get; set; }
[Display(Name = "Second Name")]
[Required(ErrorMessage = "Text is required")]
public string SecondName { get; set; }
[Display(Name = "Email")]
[Required(ErrorMessage = "Email required")]
[RegularExpression(@"^\w+([-+.]*[\w-]+)*@(\w+([-.]?\w+)){1,}\.\w{2,4}$")]
public string ContactEmail { get; set; }
在 Register.cshtml 我添加了用户输入:-
<li>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.SecondName)
@Html.TextBoxFor(m => m.SecondName)
</li>
<li>
@Html.LabelFor(m => m.ContactEmail)
@Html.TextBoxFor(m => m.ContactEmail)
</li>
然后在 AccountController.cs 我有以下内容:-
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
var UserInfo =
new
{
FirstName = model.FirstName,
SecondName = model.SecondName,
ContactEmail = model.ContactEmail
};
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, UserInfo);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
问题是当它试图做
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, UserInfo);
我收到以下错误:-
列名“FirstName”无效。
列名“SecondName”无效。
列名“ContactEmail”无效。
是因为没有使用字段创建用户表吗?如果是,我该如何创建这些字段。我已经有了初始化集
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
很感谢任何形式的帮助。
谢谢
** * *更新* ** * ** * ** * ** * ** * ** * **
这是用户配置文件
public class UserProfile : ProfileBase
{
[Display(Name = "First Name")]
public virtual string FirstName
{
get
{
return (this.GetPropertyValue("FirstName").ToString());
}
set
{
this.SetPropertyValue("FirstName", value);
}
}
[Display(Name = "Last Name")]
public virtual string LastName
{
get
{
return (this.GetPropertyValue("LastName").ToString());
}
set
{
this.SetPropertyValue("LastName", value);
}
}
[Display(Name = "Contact Email")]
public virtual string ContactEmail
{
get
{
return (this.GetPropertyValue("ContactEmail").ToString());
}
set
{
this.SetPropertyValue("ContactEmail", value);
}
}
public static UserProfile GetProfile(string username)
{
return Create(username) as UserProfile;
}
}