title: EFCore: SqliteException (Error 19) when trying out TPH
body: <p>I'm trying out a multilingual database with TPH. What I'm after is for there to only be one table that contains all strings (names, descriptions, etc.) in different languages.</p>
  <p>EFCore can generate me an Sqlite database, but when I add this <code>LanguageName</code>, it throws an <code>SqliteException</code>.</p>
  <pre class="lang-cs prettyprint-override"><code>await context.LanguageNames.AddAsync(
    new LanguageName {
      Id = LanguageName.EnglishLanguageNameId,
      Value = LanguageName.EnglishLanguageNameValue,
      ForeignEntityId = Language.EnglishLanguageId,
      LanguageId = Language.EnglishLanguageId
    });
  await context.SaveChangesAsync();
  </code></pre>
  <p>The exception says: <code>Unhandled exception. Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'FOREIGN KEY constraint failed'.</code> Googling wasn't any help in tackling this specific issue.</p>
  <p>I've tried uploading the Sqlite file to an online IDE (<a href="https://sqliteonline.com/" rel="nofollow noreferrer">this one, specifically</a>) and executing the snippet below. It worked as expected.</p>
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO MultilingualString(id, Value, foreignentityid, languageid, discriminator)
  VALUES (1, 'English', 1, 1, 'LanguageName')
  </code></pre>
  <p>So I tried running the same code in the <code>DbContext</code>.</p>
  <pre class="lang-cs prettyprint-override"><code>await context.Database.ExecuteSqlRawAsync(
    &quot;INSERT INTO MultilingualString(id, value, foreignentityid, languageid, discriminator) &quot; +
    &quot;VALUES (1, 'English', 1, 1, 'LanguageName')&quot;
  );
  </code></pre>
  <p>I still get the same <code>SqliteException</code>. What am I doing wrong?</p>
  <p>In case more details are needed, here are the models I'm working with.</p>
  <pre class="lang-cs prettyprint-override"><code>public record MultilingualString {
    public UInt64 Id { get; init; }
    public String Value { get; init; }
  
    public UInt64 ForeignEntityId { get; init; }
  
    public Language Language { get; init; }
    public UInt64 LanguageId { get; init; }
  
    protected internal static UInt64 GenerateId() { ... }
  }
  public record MultilingualString&lt;TEntity&gt; : MultilingualString
    where TEntity : class {
      public TEntity ForeignEntity { get; init; }
    }
  
  public record Language {
    public UInt64 Id { get; init; }
    public IEnumerable&lt;LanguageName&gt; Names { get; init; }
  
    public IEnumerable&lt;MultilingualString&gt; StringValues { get; init; }
  
    public static UInt64 EnglishLanguageId { get; } = 1;
  }
  public record LanguageName : MultilingualString&lt;Language&gt; {
    public static String EnglishLanguageNameValue { get; } = &quot;English&quot;;
    public static UInt64 EnglishLanguageNameId { get; } = GenerateId();
  }
  </code></pre>
  <p>Here's the model building:</p>
  <pre class="lang-cs prettyprint-override"><code>protected override void OnModelCreating(ModelBuilder modelBuilder) {
    base.OnModelCreating(modelBuilder);
    BuildMultilingualStringModel(modelBuilder)
      .BuildLanguageModel(modelBuilder)
      .BuildItemModel(modelBuilder);
  }
  protected virtual DbContext BuildMultilingualStringModel(
    ModelBuilder modelBuilder) {
      modelBuilder.Entity&lt;MultilingualString&gt;(builder =&gt; {
        builder.HasKey(@string =&gt; @string.Id);
        builder.Property(@string =&gt; @string.Value).IsRequired();
      });
      modelBuilder.Entity&lt;MultilingualString&gt;()
        .HasDiscriminator&lt;String&gt;(&quot;Discriminator&quot;)
        .HasValue&lt;LanguageName&gt;(nameof(LanguageName))
        .HasValue&lt;ItemName&gt;(nameof(ItemName))
        .HasValue&lt;ItemDescription&gt;(nameof(ItemDescription));
      return this;
    }
  protected virtual DbContext BuildLanguageModel(
    ModelBuilder modelBuilder) {
      modelBuilder.Entity&lt;Language&gt;(builder =&gt; {
        builder.HasKey(language =&gt; language.Id);
        builder.HasMany(language =&gt; language.Names)
          .WithOne(name =&gt; name.ForeignEntity)
          .HasForeignKey(name =&gt; name.ForeignEntityId);
        builder.HasMany(language =&gt; language.StringValues)
          .WithOne(@string =&gt; @string.Language)
          .HasForeignKey(@string =&gt; @string.LanguageId);
        builder.HasData(new Language {
          Id = Language.EnglishLanguageId
        });
      });
      return this;
    }
  protected virtual DbContext BuildItemModel(
    ModelBuilder modelBuilder) {
      modelBuilder.Entity&lt;Item&gt;(builder =&gt; {
        builder.HasKey(item =&gt; item.Id);
        builder.HasMany(item =&gt; item.Names)
          .WithOne(name =&gt; name.ForeignEntity)
          .HasForeignKey(name =&gt; (UInt32)name.ForeignEntityId);
        builder.HasMany(item =&gt; item.Descriptions)
          .WithOne(description =&gt; description.ForeignEntity)
          .HasForeignKey(description =&gt; description.ForeignEntityId);
      });
      return this;
    }
  </code></pre>
  <p>These are the other models in the database, just in case any of these have something to do with the error.</p>
  <pre class="lang-cs prettyprint-override"><code>public record Item {
    public UInt64 Id { get; init; }
    public IEnumerable&lt;ItemName&gt; Names  { get; init; }
    public IEnumerable&lt;ItemCategory&gt; Categories { get; init; }
    public IEnumerable&lt;ItemDescription&gt; Descriptions { get; init; }
  }
  public record ItemName : MultilingualString&lt;Item&gt; { }
  public record ItemDescription : MultilingualString&lt;Item&gt; { }
  </code></pre>
  <p>EDIT: These are the create scripts:</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE &quot;Items&quot; (
      &quot;Id&quot; INTEGER NOT NULL CONSTRAINT &quot;PK_Items&quot; PRIMARY KEY AUTOINCREMENT
  )
  
  CREATE TABLE sqlite_sequence(name,seq)
  
  CREATE TABLE &quot;Languages&quot; (
      &quot;Id&quot; INTEGER NOT NULL CONSTRAINT &quot;PK_Languages&quot; PRIMARY KEY AUTOINCREMENT
  )
  
  CREATE TABLE &quot;MultilingualStrings&quot; (
      &quot;Id&quot; INTEGER NOT NULL CONSTRAINT &quot;PK_MultilingualStrings&quot; PRIMARY KEY AUTOINCREMENT,
      &quot;Value&quot; TEXT NOT NULL,
      &quot;ForeignEntityId&quot; INTEGER NOT NULL,
      &quot;LanguageId&quot; INTEGER NOT NULL,
      &quot;Discriminator&quot; TEXT NOT NULL,
      CONSTRAINT &quot;FK_MultilingualStrings_Items_ForeignEntityId&quot; FOREIGN KEY (&quot;ForeignEntityId&quot;) REFERENCES &quot;Items&quot; (&quot;Id&quot;) ON DELETE CASCADE,
      CONSTRAINT &quot;FK_MultilingualStrings_Languages_ForeignEntityId&quot; FOREIGN KEY (&quot;ForeignEntityId&quot;) REFERENCES &quot;Languages&quot; (&quot;Id&quot;) ON DELETE CASCADE,
      CONSTRAINT &quot;FK_MultilingualStrings_Languages_LanguageId&quot; FOREIGN KEY (&quot;LanguageId&quot;) REFERENCES &quot;Languages&quot; (&quot;Id&quot;) ON DELETE CASCADE
  )
  
  CREATE INDEX &quot;IX_MultilingualStrings_ForeignEntityId&quot; ON &quot;MultilingualStrings&quot; (&quot;ForeignEntityId&quot;)
  
  CREATE INDEX &quot;IX_MultilingualStrings_LanguageId&quot; ON &quot;MultilingualStrings&quot; (&quot;LanguageId&quot;)
  </code></pre>