gpt4 book ai didi

c# - 不能向表中添加一个以上的用户。标识插入设置为关闭

转载 作者:行者123 更新时间:2023-11-29 06:27:58 25 4
gpt4 key购买 nike

这是我在单击表单上的提交以添加新用户时遇到的错误。我试图通过对用户表的查询将 IDENTITY 设置为 ON,但没有帮助。当我自己不插入数据时,我不知道如何打开和关闭它。有什么建议么?

这是我的 Controller :

    [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(User U)
{
if (ModelState.IsValid)
{
using (SomeEntities dc = new SomeEntities ())
{
//you should check duplicate registration here
dc.Users.Add(U);
dc.SaveChanges();
ModelState.Clear();
U = null;
ViewBag.Message = "Successfully Registration Done";
}
}
return View(U);
}

这是我的用户模型:

public partial class User
{
public User()
{
this.Posts = new HashSet<Post>();
this.PostFeedbacks = new HashSet<PostFeedback>();
this.ProjectUsers = new HashSet<ProjectUser>();
this.UserWithdraws = new HashSet<UserWithdraw>();
}

public decimal user_id { get; set; }

[Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
public string user_name { get; set; }

[Required(ErrorMessage="Please provide Password", AllowEmptyStrings=false)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
public string user_pass { get; set; }

[Compare("user_pass", ErrorMessage = "Confirm password dose not match.")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
public string confirm_user_pass { get; set; }

[Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
public string full_name { get; set; }

最佳答案

您是否需要明确设置身份,或者您是否可以让数据库在插入时设置身份?如果是后者,只需将 IDENTITY_INSERT 设置为 OFF 并确保标识列设置为其默认值。

此外,请确保标记您的身份列。例如。

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID {get; protected set;}

关于c# - 不能向表中添加一个以上的用户。标识插入设置为关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754735/

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