I have a table and I want to insert items into it without specifying the UserID. I want this ID to be generated automatically.
CREATE TABLE [dbo].[UserMaster]
(
[UserID] INT NOT NULL ,
[UserName] VARCHAR (50) NULL,
[UserPassword] VARCHAR (50) NULL,
[UserRoles] VARCHAR (500) NULL,
[UserEmailID] VARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC)
);
Here I try to insert row via C#:
using (LessonesDBEntities context = new LessonesDBEntities())
{
var u = new UserMaster//Make sure you have a table called UserMaster in DB
{
UserName = "test username",
UserPassword = "test password",
UserEmailID = "test email",
UserRoles = "User"
};
context.UserMaster.Add(u);
context.SaveChanges();
}
But I get exception:
Violation of PRIMARY KEY constraint 'PK__tmp_ms_x__1788CCACDB8355D2'. Cannot insert duplicate key in object 'dbo.UserMaster'. The duplicate key value is (0). The statement has been terminated.