我插入用户时,标识正在尝试复制电子邮件属性

时间:2017-05-18 11:43:27

标签: c# asp.net-mvc razor asp.net-identity

所以我有一个异步的Task方法,其中类型T = ActionResult来创建一个要存储在数据库中的新用户(DBMS = SQL Server)。这曾经在没有任何错误之前完美地工作,但突然间它在执行到这一点时开始破坏:

 var result = await UserManager.CreateAsync(user, model.Password);

我已经将所有内容恢复到Git上的工作版本但仍然没有运气。

可能是什么问题,我该如何解决这个问题?

控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser()
        {
            UserName = model.UserName,
            firstName = model.firstName,
            lastName = model.lastName,
            Email = model.email,
            cellNo = model.cellNo,
            DateCreated = DateTime.Now
        };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            //await SignInAsync(user, isPersistent: false);
            //  util.Log(User.Identity.GetUserId(), 5, "Account - Added a user ( " + model.UserName + " ) to the system");
            return RedirectToAction("SuccessfullyAddedNewUser", "Users", model);
        }
        else
        {
            AddErrors(result);
        }
    }
    // If we got this far, something failed, redisplay form
    return View(model);
}

//Users controller action method to get redirected to once new user has been created successfully
// GET: /Users/SuccessfullyAddedNewUser
public ActionResult SuccessfullyAddedNewUser(RegisterViewModel model)
{
    return View(model);
}

型号:ApplicationUser

public class ApplicationUser : IdentityUser
{

    [Required]
    [StringLength(11, MinimumLength = 11)]
    [Display(Name = "Cell Number")]
    public string cellNo { get; set; }

    [Required]
    [MaxLength(50), MinLengthAttribute(3)]
    [Display(Name = "First Name")]
    public string firstName { get; set; }


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

    [Required]
    [MaxLength(50), MinLengthAttribute(3)]
    [Display(Name = "Last Name")]
    public string lastName { get; set; }

    [Display(Name = "Date Created")]
    public DateTime DateCreated { get; set; }

    [Required]
    [DefaultValue(false)]
    public Boolean MustResetPassword { get; set; }

    //Concatenates the first and last name
    public String getFullName()
    {
        return firstName + " " + lastName;
    }
}

例外:

  

异常:Message =“更新条目时发生错误。有关详细信息,请参阅内部异常。”

     

内部异常:在INSERT的SET子句或列列表中多次指定列名“email”。列不能   在同一子句中分配了多个值。将子句修改为   确保列只更新一次。如果这句话   更新或插入列到视图中,列别名可以隐藏   代码中的重复。

但是当我检查我的代码时,我只指定一次电子邮件。

1 个答案:

答案 0 :(得分:2)

IdentityUser已有Email属性。无论如何,ApplicationUser都会将该属性与email重复。

删除重复的属性

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

来自ApplicationUser或使用

覆盖它
[Display(Name = "Email Address")]
public new string Email { get; set; }

如果只是为了添加display属性。