【问题标题】:WebSecurity.CreateUserAndAccount throw Cannot convert type 'string' to 'int'WebSecurity.CreateUserAndAccount throw 无法将类型“string”转换为“int”
【发布时间】:2014-08-23 16:18:05
【问题描述】:

我需要使用实体框架创建一个注册页面,并且我尝试将 websecurity 类用于 userAuth 系统我编码用于注册这是代码

public ActionResult Register(RegisterUser model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.Username, model.Password,
                new { FullName = model.FullName,
                      Email = model.Email,
                      Phone = model.Phone });
                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);
    }

这是我的模型

    public class RegisterUser{
    [Required]
    [Display(Name = "User Name")]
    public string Username { get; set; }

    [Required]
    [StringLength(15, ErrorMessage = "The {0} must be at least {2} characters long and upto 15 characters", MinimumLength = 6)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Display(Name="Full Name")]
    public string FullName { get; set; }

    [Display(Name="Email")]
    public string Email { get; set; }

    [Display(Name="Phone Number")]
    [StringLength(12,ErrorMessage = "Wrong Phone number",MinimumLength = 9)]
    public string Phone { get; set; }
}

当我运行并注册一些我得到的数据时 用户代码未处理 RuntimeBinderException 无法将类型 'string' 转换为 'int'

我尝试检查我的值是所有字符串,我的模型也是字符串 我不明白为什么程序会那样抛出 任何人都可以帮助我吗?

【问题讨论】:

  • 你的数据库中的Phone字段是什么数据类型?
  • @AmarDuplantier 电话是字符串类型,我尝试将每一列都更改为字符串,但仍然出错

标签: c# asp.net-mvc entity-framework


【解决方案1】:

我有同样的错误。就我而言,问题出在 WebSecurity 初始化 WebSecurity.InitializeDatabaseConnection();

您必须为 userIdColumn 使用任何字符串字段。

用户标识列
包含用户 ID 的数据库列的名称。此列必须输入为整数(int)

http://msdn.microsoft.com/en-us/library/gg569134(v=vs.111).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多