title: DbUpdateException (insert statement conflicted with foreign key constraint) when adding image (byte array) to database
body: <p>A DbUpdateException is being thrown at <code>db.SaveChanges()</code> in my <code>UploadReferencePhoto ActionResult</code> when I try to upload a photo to a SQL database via my application. I'm using code first with table-per-type inheritance. Models related to the scenario are as follows:</p>
  
  <p>UserProfile:</p>
  
  <pre><code>[Table("UserProfile")]
  public class UserProfile
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int UserId { get; set; }
      // ...
  }
  </code></pre>
  
  <p>Tenant:</p>
  
  <pre><code>[Table("Tenant")]
  public class Tenant : UserProfile
  {
      public Tenant()
      {
          this.ReferencePhotos = new List&lt;ReferencePhoto&gt;();
      }
  
      public virtual ICollection&lt;ReferencePhoto&gt; ReferencePhotos { get; set; }
  }
  </code></pre>
  
  <p>Image:</p>
  
  <pre><code>[Table("Image")]
  public class Image
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int ImageId { get; set; }
  }
  </code></pre>
  
  <p>ReferencePhoto:</p>
  
  <pre><code>[Table("ReferencePhoto")]
  public class ReferencePhoto : Image
  {
      // 1:many with Tenant
      public int UserId { get; set; }
      [ForeignKey("UserId")]
      public virtual Tenant Tenant { get; set; }
  }
  </code></pre>
  
  <p>So <code>Tenant</code> extends <code>UserProfile</code> and <code>ReferencePhoto</code> extends <code>Image</code>. When I click upload aforementioned exception is thrown. Inner exception is as follows:</p>
  
  <blockquote>
    <p>"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.ReferencePhoto_dbo.Tenant_UserId\". The conflict occurred in database \"C:\USERS\HOME\DESKTOP\LETLORD\LETLORD\APP_DATA\LETLORD.MDF\", table \"dbo.Tenant\", column 'UserId'.\r\nThe statement has been terminated."}</p>
  </blockquote>
  
  <p>Can someone tell me what exactly the inner exception is saying and possibly how to resolve it? Let me know if more code/information needed.</p>