處理我有一個名爲測試一個數據庫,其中包括所謂的用戶信息保存用戶的信息在我的網站一個表例外在db.SaveChanges()在asp.net MVC
用戶信息表包含
1.ID:主鍵 2.name 3.password
http://s4.postimg.org/ut37i6y6x/window1.png
namespace FirstApp.Models
{
using System;
using System.Collections.Generic;
public partial class userinfo
{
public int ID { get; set; }
public string name { get; set; }
public string password { get; set; }
}
}
我修改C帳戶ontroller如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FirstApp.Models;
namespace FirstApp.Controllers
{
public class AccountController : Controller
{
public TestEntities3 db = new TestEntities3();
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Reg()
{
return View();
}
[HttpPost]
public ActionResult Reg(Models.userinfo regclass)
{
var newuser = db.userinfoes.Create();
db.userinfoes.Add(regclass);
db.SaveChanges();
return View(regclass);
}
}
}
和圖如下所示:
@model FirstApp.Models.userinfo
@{
ViewBag.Title = "Reg";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>This is Registration page </h2>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(u=>u.name)
@Html.TextBoxFor(u=>u.name)
</div>
<div>
@Html.LabelFor(u=>u.password)
@Html.PasswordFor(u=>u.password)
</div>
<input type="submit" />
}
當我測試上面的代碼是在只有第一用戶的工作,但,當我試圖進入第二用戶以下異常發生:
http://s11.postimg.org/5hg84m16b/problem1.png
以另一種嘗試此錯誤消息是出現:
無法將重複值插入到唯一索引中。 [Table name = userinfo,Constraint name = PK_userinfo]
你能發佈你的實體如何配置? – KnightFox