title: Auto increment primary key with Entity Framework
body: <p>I have a table and I want to insert items into it without specifying the <code>UserID</code>. I want this ID to be generated automatically.</p>
  
  <pre><code>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)
  );
  </code></pre>
  
  <p>Here I try to insert row via C#:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>But I get exception:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>