title: C# Cannot insert explicit value for identity column in table "Rentals" when IDENTITY_INSERT is set to OFF
body: <p>I am currently learning ASP.NET MVC and I'm learning to get an object via its ID and inserting its reference to the database. However, I am getting this error </p>
  
  <blockquote>
    <p>Cannot insert explicit value for identity column in table "Rentals" when IDENTITY_INSERT is set to OFF</p>
  </blockquote>
  
  <p>I have read around and I noticed that some people say to set identity insert to off because the ID of the referenced table is auto incremented? However I have read that this method is not recommended because it apparently locks off the table or is only useful for single users? May I know what is the correct way to do this?</p>
  
  <p>This is my controller code that is trying to add a new Rental</p>
  
  <pre><code>[HttpPost]
  public IHttpActionResult CreateNewRental(RentalDTO RentalDTO)
  {
      if (RentalDTO.MovieIds.Count == 0)
      {
          return BadRequest();
      }
      var customer = _context.Customers.SingleOrDefault(c =&gt; c.Id == RentalDTO.CustomerId);
      if (customer == null)
      {
          return BadRequest("Customer ID is not valid");
      }
      var movies = _context.Movies.Where(m =&gt; RentalDTO.MovieIds.Contains(m.Id)).ToList();
      if (movies.Count != RentalDTO.MovieIds.Count)
      {
          return BadRequest();
      }
      foreach (var movie in movies)
      {
          if (movie.NumberAvailable &lt; 1)
          {
               return BadRequest();
          }
          movie.NumberAvailable--;
          var rental = new Rentals
          {
              Customer = customer,
              Movie = movie,
              DateRented = DateTime.Now,
          };
          _context.Rentals.Add(rental);
      }
      _context.SaveChanges();
      return Ok();
  }
  </code></pre>
  
  <p>This is my rentals model</p>
  
  <pre><code>public class Rentals
  {
      public byte Id { get; set; }
      [Required]
      public Customers Customer { get; set; }
      [Required]
      public Movies Movie { get; set; }
      public DateTime DateRented { get; set; }
      public DateTime? DateReturned { get; set; }
  }
  </code></pre>
  
  <p>This is the migration I used to create the table</p>
  
  <pre><code>CreateTable(
              "dbo.Rentals",
              c =&gt; new
                  {
                      Id = c.Byte(nullable: false, identity: true),
                      DateRented = c.DateTime(nullable: false),
                      DateReturned = c.DateTime(),
                      Customer_Id = c.Int(nullable: false),
                      Movie_Id = c.Int(nullable: false),
                  })
              .PrimaryKey(t =&gt; t.Id)
              .ForeignKey("dbo.Customers", t =&gt; t.Customer_Id, cascadeDelete: true)
              .ForeignKey("dbo.Movies", t =&gt; t.Movie_Id, cascadeDelete: true)
              .Index(t =&gt; t.Customer_Id)
              .Index(t =&gt; t.Movie_Id);
  </code></pre>
  
  <p>UPDATE: </p>
  
  <p>I have debugged my code and realized that when I added a new Rental, 0 was set as the ID. How do I set that automatically?</p>