title: Web API CORE: "FOREIGN KEY constraint failed" error during POST request SQLite
body: <p>I am using .Net Web API core and Entity Framework Core to build an API for a product store. My Product entity looks like this</p>
  
  <pre><code>public class Product
  {
      [Key]
      public long ID { get; set; }
  
      [Required]
      [MaxLength(80)]
      public string Name { get; set; } 
      ............
  }
  </code></pre>
  
  <p>I have Order entity which is</p>
  
  <pre><code>public class Order
  {
      [Key]
      public long ID { get; set; }
      public List&lt;OrderItem&gt; OrderItems { get; set; }
      ..........
  }
  </code></pre>
  
  <p>And I have OrderItem Entity, which is created when the user adds the product to the Card, but when no Order is created yet:</p>
  
  <pre><code> public class OrderItem
  {
      [Key]
      public long ID { get; set; }
  
      public string UID { get; set; }
  
      public long OrderID { get; set; }
      public Order Order { get; set; }
  
      [Required]
      public long ProductID { get; set; }
      public Product Product { get; set; }
  
      public int Quantity { get; set; }
  }
  </code></pre>
  
  <p>It tells that the user has added the Product with ProductID(FK) with THIS Quantity to his card. But no order is created yet(so OrderID FK is null yet). </p>
  
  <p>And when I make the post request for adding OrderItem with ProductID, Quantity and UID to my database, like this :</p>
  
  <pre><code>[HttpPost]
      public IActionResult Post([FromBody]JObject newItem)
      {
          OrderItem oItem = newItem.ToObject&lt;OrderItem&gt;();
  
          context.OrderItems.Add(oItem);
          context.SaveChanges();
          return StatusCode(200);
      }
  </code></pre>
  
  <p>an exception is thrown with the text 'FOREIGN KEY Constraint failed" on the line where I call SaveChanges(). I am adding an object with valid ProductID, the JObject is casted normally. I tried adding the same object with SQLIte browser manually, it didnt give any errors. Concerning OrderID, to which I dont give a value yet, foreign keys CAN be null, cant they? </p>
  
  <p>Please help me, I can't understand what causes this issue.
  P.S. I use Visual Studio 2017 on Mac OS. </p>
  
  <p><strong>UPD.</strong></p>
  
  <p>here is the output(I consoled the JObject to see if I get it right):</p>
  
  <pre><code>    Application started. Press Ctrl+C to shut down.
  Jobject received is:{
    "ProductID": 2,
    "UID": "6c523a76-dd6e-7782-2bc9-9b8baba36e5d",
    "Quantity": "1"
  }
  OrderItem casted is: ProductID=2; UID=6c523a76-dd6e-7782-2bc9-9b8baba36e5d
  fail: Microsoft.EntityFrameworkCore.DbContext[1]
        An exception occurred in the database while saving changes.
        Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---&gt; Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.
           at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db)
           at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
           at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
           at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
           at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
           --- End of inner exception stack trace ---
           at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
           at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
           at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
           at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
           at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
  Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---&gt; Microsoft.Data.Sqlite.SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed'.
     at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db)
     at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
     at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
     at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
     at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
     --- End of inner exception stack trace ---
     at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
     at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)
     at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
     at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
     at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
  </code></pre>