gpt4 book ai didi

c# - WebSecurity.CreateUserAndAccount "Field does not exist"

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:38 27 4
gpt4 key购买 nike

我正在尝试扩展简单成员资格以包括名字、第二名和电子邮件地址,但是我在使用 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;
}
}

最佳答案

您需要将这三个新字段(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 "Field does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17586637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com