C# WebSecurity.CreateUserAndAccount";“字段不存在”;
我正在尝试扩展简单的成员资格,以包括FirstName、SecondName和电子邮件地址,但是我在CreateUserAndAccount中遇到了一些问题 因此,基本上在RegisterModel中,我添加了以下内容:-C# WebSecurity.CreateUserAndAccount";“字段不存在”;,c#,c#-4.0,asp.net-mvc-4,entity-framework-5,C#,C# 4.0,Asp.net Mvc 4,Entity Framework 5,我正在尝试扩展简单的成员资格,以包括FirstName、SecondName和电子邮件地址,但是我在CreateUserAndAccount中遇到了一些问题 因此,基本上在RegisterModel中,我添加了以下内容:- [Display(Name = "First Name")] [Required(ErrorMessage = "Text is required")] public string FirstName { get; set; } [Displ
[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; }
[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);
}
在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>
问题是,当它试图做到
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, UserInfo);
我发现以下错误:-
[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; }
[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);
}
列名“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;
}
}
您需要将这三个新字段(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; }
}
请确保删除表,以便删除以前的表。问题在于,在对模型执行这些更改之前,您使用了简单成员身份。这导致SimpleMembership使用默认列(id和名称)创建了
UserProfile
表
在这种情况下,设置autoCreateTables:true
没有帮助,因为您的表存在!但是他们没有新的专栏
因此,您需要删除这4个表,让db初始值设定项为您创建新的有效表。我已经在UserProfile中添加了它们!请参阅更新的问题在进一步调查之后,我决定使用两个不同的表,并保持成员表的完整性。