title: IDENTITY_INSERT is set to OFF - Visual Studio
body: <p>I'm trying to insert values into a table in a local database. I'm using the EntityFramework for this.
  My code is pretty simple and straight forward, however I'm getting this error, when I try to insert data into the table.</p>
  
  <pre><code>Cannot insert explicit value for identity column in table 'PUserDataTable' when IDENTITY_INSERT is set to OFF.
  </code></pre>
  
  <p>Update:
  I'm getting this error with the below code now.</p>
  
  <pre><code>Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace SampleDatabase.Controllers
  {
      public class PUserDataTableController : Controller
      {
          PUserDataTableContext db = new PUserDataTableContext();
  
          public ActionResult Index()
          {
              return View(db.PUserDataTables.ToList());
          }
  
          [HttpGet]
          public ActionResult Create()
          {
              return View();
          }
          [HttpPost]
  
          public ActionResult Create(PUserDataTable userdata)
          {
             if(ModelState.IsValid)
             {
              db.PUserDataTables.Add(userdata);
              try
              {
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              catch (DbEntityValidationException e)
              {
                  foreach (var eve in e.EntityValidationErrors)
                  {
                      Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                      foreach (var ve in eve.ValidationErrors)
                      {
                          Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                              ve.PropertyName, ve.ErrorMessage);
                      }
                  }
                  throw;
              }
  
             }
              return View(userdata);
          }
      }
  }
  </code></pre>
  
  <p>Here is my Model:</p>
  
  <pre><code>namespace SampleDatabase
  {
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations.Schema;
  
      public partial class PUserDataTable
      {
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public Nullable&lt;int&gt; Id { get; set; }
          public string DeviceID { get; set; }
          public string TimeSpent { get; set; }
          public Nullable&lt;int&gt; NumPages { get; set; }
          public string History { get; set; }
          public string FinalPage { get; set; }
  
      }
  }
  </code></pre>
  
  <p>Here is how I create my table.</p>
  
  <pre><code>CREATE TABLE [dbo].[PUserDataTable] (
      [Id]        INT           IDENTITY (1, 1) NOT NULL,
      [DeviceID]  VARCHAR (50)  NULL,
      [TimeSpent] VARCHAR (50)  NULL,
      [NumPages]  INT           NULL,
      [History]   VARCHAR (MAX) NULL,
      [FinalPage] VARCHAR (50)  NULL,
      CONSTRAINT [PK_PUserDataTable] PRIMARY KEY CLUSTERED ([Id] ASC)
  );
  </code></pre>
  
  <p>How do I resolve this error? Any help will be appreciated. </p>