title: Design DB where primary key of table is also foreign key of another table
body: <p>I am trying to figure out the best way to design my database. I have 2 tables, <code>Product</code> and <code>Image</code>. </p>
  
  <p>Product </p>
  
  <pre><code>PId PRIMARY KEY IDENTITY(1, 1)
  PName
  PPrice
  </code></pre>
  
  <p>Image </p>
  
  <pre><code>PId PRIMARY KEY IDENTITY(1,1)
  PImage1 
  PImage2
  FOREIGN KEY (PId) REFERENCES Product(PId)
  </code></pre>
  
  <p>Currently my database is designed in a bad way. Since I assume that whenever an entry into <code>Product</code> is added, right after an entry into <code>Image</code> is added. This assumption also makes the assumption that the generated primary key for <code>Image</code> is the same as the generated primary key for <code>Product</code>.</p>
  
  <p>Then in the controller code I have:</p>
  
  <pre><code>_context.Product.Add(Product); // Add the created product record to the database
  
  await _context.SaveChangesAsync();
  
  _context.Image.Add(imageRecord); // Add the created image record to the database.
  
  await _context.SaveChangesAsync();
  </code></pre>
  
  <p>where, respectively, <code>Product</code> and <code>imageRecord</code> are objects of the following model classes:</p>
  
  <pre><code>public class Product
  {
      public int ProductID { get; set; }
      public string ProductName { get; set; }
      public string ProductShortDesc { get; set; }
      public string ProductLongDesc { get; set; }
      public decimal ProductPrice { get; set; }
  }
  
  public class Image
  {
      [ForeignKey("Product")]
      [Key]
      public int ProductID { get; set; }
      public byte[] ProductImage1 { get; set; }
      public byte[] ProductImage2 { get; set; }
      public byte[] ProductImage3 { get; set; }
  }
  </code></pre>
  
  <p>This begins to fail when I start deleting entries from the tables, and the auto-incremented primary key values no longer are the same.</p>
  
  <p>How can I design it better so that I am able to choose the primary key of <code>Product</code>, enabling me to then use this as the primary key of <code>Image</code>?</p>