title: EF Core - create relationship without primary/foreign keys
body: <p>I'm trying to configure EF to include documents when retriving a user or product. The entity Document has a ReferenceId property which should store either UserId or ProductId. This way, when I save a document for a user or product, the UserId or ProductId is saved to <em>Document.ReferenceId</em>.</p>
  <p>Entities:</p>
  <pre>public class User
  {
      public string Id { get; set; }
      public ICollection&lt;Document&gt; Documents { get; set; }
  }
  
  public class Product
  {
      public string Id { get; set; }
      public ICollection&lt;Document&gt; Documents { get; set; }
  }
  
  public class Document
  {
      public string Id { get; set; }
      public string ReferenceId { get; set; }
  }
  </pre>
  <p>Configuring:</p>
  <pre>builder.Entity&lt;User&gt;(e =&gt;
  {
      e.HasKey(e =&gt; e.Id);
      e.Property(p =&gt; p.Id).ValueGeneratedOnAdd();
      e.HasMany(e =&gt; e.Documents)
       .WithOne()
       .OnDelete(DeleteBehavior.Cascade);
  });
  
  builder.Entity&lt;Product&gt;(e =&gt;
  {
      e.HasKey(e =&gt; e.Id);
      e.Property(p =&gt; p.Id).ValueGeneratedOnAdd();
      e.HasMany(e =&gt; e.Documents)
       .WithOne()
       .OnDelete(DeleteBehavior.Cascade);
  });
  
  builder.Entity&lt;Document&gt;(e =&gt;
  {
      e.HasKey(e =&gt; e.Id);
      e.Property(p =&gt; p.Id).ValueGeneratedOnAdd();
      e.ToTable(&quot;Documents&quot;);
  });
  </pre>
  <p>Saving:</p>
  <pre>var user = new User { };
  var userDocument = new Document { ReferenceId = user.Id };
  
  var product = new Product { };
  var productDocument = new Document { ReferenceId = product.Id };
  
  _context.Users.Add(user);
  _context.Products.Add(product);
  _context.Add(userDocument);
  _context.Add(productDocument);
  _context.SaveChanges();
  </pre>
  <p>Migrations:</p>
  <pre>migrationBuilder.CreateTable(
      name: &quot;Documents&quot;,
      columns: table =&gt; new
      {
          Id = table.Column&lt;string&gt;(nullable: false),
          ReferenceId = table.Column&lt;string&gt;(nullable: true),
          ProductId = table.Column&lt;string&gt;(nullable: true),
          UserId = table.Column&lt;string&gt;(nullable: true)
      },
      constraints: table =&gt;
      {
          table.PrimaryKey(&quot;PK_Documents&quot;, x =&gt; x.Id);
          table.ForeignKey(
              name: &quot;FK_Documents_Products_ProductId&quot;,
              column: x =&gt; x.ProductId,
              principalTable: &quot;Products&quot;,
              principalColumn: &quot;Id&quot;,
              onDelete: ReferentialAction.Cascade);
          table.ForeignKey(
              name: &quot;FK_Documents_Users_UserId&quot;,
              column: x =&gt; x.UserId,
              principalTable: &quot;Users&quot;,
              principalColumn: &quot;Id&quot;,
              onDelete: ReferentialAction.Cascade);
      });
  </pre>
  <p>I don't want 2 foreign keys (ProductId and UserId) to be created on Documents table. Is there a way to make EF automatically link UserId and ProductId to ReferenceId?</p>