我试图将简单成员资格扩展为包括名字,第二名和电子邮件地址,但是我在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);
我收到以下错误:-
无效的列名“名字”。
无效的列名“ SecondName”。
无效的列名“ ContactEmail”。
是否因为未使用字段创建User表?如果是这样,我如何创建这些字段。我已经有初始化集
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
很感谢任何形式的帮助。
谢谢
****更新*********************
这是UserProfile
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;
}
}
最佳答案
您需要将这三个新字段(FirstName,SecondName和ContactEmail)添加到继承DbContext的UserProfile类中。
UserProfile类在您的帐户模型中定义。
[Table("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 SecondName { get; set; }
public string ContactEmail { get; set; }
}
确保删除您的表,以便删除以前的表。
关于c# - WebSecurity.CreateUserAndAccount“字段不存在”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17586637/