I am quite new to ASP.Net MVC. I have just started learning it so kindly help me as I am stuck at a place. Early response will be highly appreciated. I am making a registration page. Added a SQL database using App_data .Below is the table definition :
CREATE TABLE [dbo].[RegisterModel]
(
[Id] INT NOT NULL primary key ,
[UserName] VARCHAR(50) NOT NULL,
[Email] VARCHAR(50) NOT NULL,
[Password] VARCHAR(50) NOT NULL,
[ConfirmPassword ] VARCHAR(50) NOT NULL,
)
Then added ADO.Net Entity data model for the same. then I created a controller Account with an action method Index as below:
public class AccountController : Controller
{
//
// GET: /Account/
public ActionResult Index()
{
//using (dbRegisterModelEntities dc = new dbRegisterModelEntities())
//{
return View();
}
[HttpPost]
public ActionResult Index(RegisterModel user)
{
if (ModelState.IsValid)
{
using (dbRegisterModelEntities dc = new dbRegisterModelEntities())
{
dc.RegisterModels.Add(user);
dc.SaveChanges();
return RedirectToAction("Index", "Account");
}
}
return View(user);
}
After creating one user , now I am getting error :
Violation of PRIMARY KEY constraint 'PK__tmp_ms_x__3214EC0778D493BE'. Cannot insert duplicate key in object 'dbo.RegisterModel'. The duplicate key value is (0). The statement has been terminated.
This error is coming when I am trying to add a second user.