title: Insert new entity with EF Core with a manually assigned Id
body: <p>I need to insert an enitity into the database that has a constraint on it's id, so I want to set it manually.</p>
  
  <p><em>Note: The model below is the minimal example to get the idea of what I'm doing. The whole thing is running on ASP.NET Core 2 with EF Core and Pomelo MySql provider.</em></p>
  
  <p>I'm trying to create an entity in my already existing database (MySQL) that is defined like this:</p>
  
  <pre><code>CREATE TABLE Users (
      id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
      email VARCHAR(128) NOT NULL);
  
  CREATE TABLE UserData (
      id INTEGER PRIMARY KEY,
      name VARCHAR(32) NOT NULL,
      FOREIGN KEY(id) REFERENCES Users(id));
  </code></pre>
  
  <p>So each record in the <code>Users</code> table has a record in the <code>UserData</code> table.
  Now in my code I want to use something like</p>
  
  <pre><code>Context.Users.Add(user);
  Context.SaveChanges();
  data.Id = user.Id;
  Context.UserData.Add(data);
  Context.SaveChanges();
  </code></pre>
  
  <p>The <code>User</code> should get their <code>Id</code> generated by the database and then their <code>UserData</code> created with that <code>Id</code>. However, the statement executed by EF results in an error:</p>
  
  <pre><code>Failed executing DbCommand (6ms) [Parameters=[@p0='?', @p1='?' (Size = 32)], CommandType='Text', CommandTimeout='30']
    INSERT INTO `UserData` (`id`, `name`)
    VALUES (@p0, @p1);
  
  MySql.Data.MySqlClient.MySqlException: Cannot add or update a child row:
    a foreign key constraint fails (`Main`.`UserData`, CONSTRAINT `UserData_ibfk_1`
    FOREIGN KEY (`id`) REFERENCES `Users` (`id`))
  </code></pre>
  
  <p>Which means that whatever <code>Id</code> is passed to MySQL it's not the one of <code>User</code>.</p>
  
  <p>I even tried adding a <code>[DatabaseGenerated(DatabaseGeneratedOption.None)]</code> to the <code>UserData.Id</code> as suggested in <a href="https://stackoverflow.com/questions/18907411/entering-keys-manually-with-entity-framework">Entering keys manually with Entity Framework</a>, but I'm thinking that is more with regard to CodeFirst approach rather than having an already running database.</p>
  
  <p>From this question: <a href="https://stackoverflow.com/questions/41732585/ef-core-add-function-returns-negative-id">EF Core Add function returns negative id</a> I learned that <code>Context.Add()</code> modifies the <code>Id</code>, but assigning my value after <code>Add</code> doesn't change anything.</p>
  
  <p>In case it's important, here's my DbContext Configuration</p>
  
  <pre><code>modelBuilder.Entity&lt;UserData&gt;(entity =&gt;
  {
      entity.Property(e =&gt; e.Id)
          .HasColumnName("id")
          .HasColumnType("int(11)")
          .ValueGeneratedNever();
  
      entity.Property(e =&gt; e.Name)
          .IsRequired()
          .HasColumnName("name")
          .HasMaxLength(32);
  
      entity.HasOne(d =&gt; d.User)
          .WithOne(p =&gt; p.UserData)
          .HasForeignKey&lt;UserData&gt;(d =&gt; d.Id)
          .OnDelete(DeleteBehavior.ClientSetNull)
          .HasConstraintName("UserData_ibfk_1");
  }
  modelBuilder.Entity&lt;User&gt;(entity =&gt;
  {
      entity.Property(e =&gt; e.Id)
          .HasColumnName("id")
          .HasColumnType("int(11)")
          .ValueGeneratedNever();
  
      entity.Property(e =&gt; e.Email)
          .IsRequired()
          .HasColumnName("email")
          .HasMaxLength(128);
  }
  </code></pre>