title: Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core)
body: <p>I'm getting this error when I attempt to add a new entity into the Database. The entity name is <code>DestuffedContainer</code>. The definition of this entity and related entities is below:</p>
  
  <p>DestuffedContainer:</p>
  
  <pre><code>[Table("DestuffedContainer")]
  public class DestuffedContainer
  {
      public long DestuffedContainerId { get; set; }
      public int Index { get; set; }
      public string Description { get; set; }
      public int? PackageQuantity { get; set; }
      public string PackageType { get; set; }
      public double? CBM { get; set; }
      public string Location { get; set; }
      public string MarksAndNumber { get; set; }
      public int? ManifestWeight { get; set; }
      public int? FoundWeight { get; set; }
      public int? ManifestQuantity { get; set; }
      public string ConsigneeName { get; set; }
      public string Remarks { get; set; }
      public string InvoiceFound { get; set; }
      public string PackageFound { get; set; }
      public long TellySheetId { get; set; }
      public TellySheet TellySheet { get; set; }
      public long ContainerIndexId { get; set; }
      public ContainerIndex ContainerIndex { get; set; }
  }
  </code></pre>
  
  <p>TellySheet:</p>
  
  <pre><code>[Table("TellySheet")]
  public class TellySheet
  {
      public TellySheet()
      {
          DestuffedContainers = new List&lt;DestuffedContainer&gt;();
      }
  
      public long TellySheetId { get; set; }
      public string TellyClerk { get; set; }
      public DateTime? DestuffDate { get; set; }
      public string DayNight { get; set; }
      public long ShippingAgentId { get; set; }
      public ShippingAgent ShippingAgent { get; set; }
  
      public List&lt;DestuffedContainer&gt; DestuffedContainers { get; set; }
  }
  </code></pre>
  
  <p>ContainerIndex:</p>
  
  <pre><code>[Table("ContainerIndex")]
  public class ContainerIndex
  {
      public long ContainerIndexId { get; set; }
      public string BLNo { get; set; }
      public int? IndexNo { get; set; }
      public double? BLGrossWeight { get; set; }
      public string Description { get; set; }
      public string MarksAndNumber { get; set; }
      public string ShippingLine { get; set; }
      public bool? IsDestuffed { get; set; }
      public string AuctionLotNo { get; set; }
      public long? ContainerId { get; set; }
  
      public Container Container { get; set; }
      public DeliveryOrder DeliveryOrder { get; set; }
      public OrderDetail OrderDetail { get; set; }
      public DestuffedContainer DestuffedContainer { get; set; }
      public Auction Auction { get; set; }
  }
  </code></pre>
  
  <p>The error occurs in the below lines of code when I try to add the list of destuffed containers:</p>
  
  <pre><code> var dstfContainers = new List&lt;DestuffedContainer&gt;();
   _tellySheetRepo.Add(tellySheet);
  
   foreach (var container in containers)
   {
       var destuff = new DestuffedContainer
              {
                  TellySheetId = tellySheet.TellySheetId,
                  ContainerIndexId = container.ContainerIndexId,
                  Index = container.IndexNumber ?? 0,
                  Description = container.Description,
                  PackageQuantity = container.Package,
                  PackageType = container.PackageType,
                  CBM = container.CBM,
                  ManifestWeight = container.ManifestWeight &gt; 0 ? Convert.ToInt32(container.ManifestWeight) : 0,
                  FoundWeight = container.FoundWeight &gt; 0 ? Convert.ToInt32(container.FoundWeight) : 0,
                  MarksAndNumber = container.MarksAndNumber,
                  Location = container.Location,
                  Remarks = container.Remarks,
                  InvoiceFound = container.InvoiceFoud,
                  PackageFound = container.PackageFoud
              };
  
      dstfContainers.Add(destuff);
  
      var index = _cIndexRepo.Find(container.ContainerIndexId);
  
      if (index != null)
      {
          index.IsDestuffed = true;
          _cIndexRepo.Update(index);
      }
  }
  
  _destuffRepo.AddRange(dstfContainers);
  </code></pre>
  
  <p>I'm not sure what this error means as I'm not explicitly specifying the primary key value of the destuffedcontainer entity and by default it's 0. Entity Framework should pick this up as an insert but instead it throws an error.</p>
  
  <p>It was working fine few days ago but I'm not sure what has changed since causing this error. </p>
  
  <p>I'm using Entity Framework Core for modeling my entities. I've tried several solutions but none of them seem to work. I'd appreciate any help to resolve this issue.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It seems that when I assign the ContainerIndexId value which is the foreign key in DestuffedContainer table to the entity, I get this error. I'm not sure how it's relevant.</p>