title: How should I define a composite primary key for an existing database table?
body: <p>I'm working on a Core MVC project that reads data (no writing required) from a pre-existing database. Unfortunately this database is a total mess, but I cannot change anything in it (and even if I could, I wouldn't touch it with a 10-foot pole).</p>
  
  <p>Some of the relevant problems of the database are the following:</p>
  
  <ul>
  <li>The tables don't have any foreign key relations to each other, and contain data that would have best been entered into a sub-table; whoever created the database seems to have used tables as if they were Excel spreadsheets.</li>
  <li>There are no primary or foreign keys defined.</li>
  <li>The names of tables and columns seem to be the initials of the data contained (for an imaginary example, a table that one would normally name "Customer" would instead be named "dbbc" for "Database - Business Customer").</li>
  <li>The tables are not in the "dbo" schema.</li>
  </ul>
  
  <p>Nevertheless I am forced to read data from this database and would prefer to use Entity Framework to do so.</p>
  
  <p>The database is hosted in <strong>SQL Server 2008R2</strong>.</p>
  
  <p>I could make it work with no issue on one of the tables I have to use, by using attributes such as [Table], [Column] and [Key] to use a class with proper property names mapped to the real column names. Another table however is proving to be a problem.</p>
  
  <p>This table doesn't have a single column that could be considered a primary key, but I found that a combination of two columns <strong>is</strong> unique for each row, so they could be considered a composite primary key (even if they're not defined as such in the database).</p>
  
  <p>I've defined a class like the following (I've changed the names to hide the identity of this database, as much as I'd like to name and shame):</p>
  
  <pre><code>[Table("dbbo", Schema = "schema")]
  public class Order
  {
      [Key]
      [Column("order", Order = 0)]
      public string OrderNo { get; set; }
  
      [Key]
      [Column("order_line", Order = 1)]
      public string OrderLineNo { get; set; }
  
      [Column("qty")]
      public double Quantity { get; set; }
  
      [Column("pr")]
      public double Price { get; set; }
  }
  </code></pre>
  
  <ul>
  <li>I've added a [Key] attribute to both columns that make up the imaginary composite primary key.</li>
  <li>I've added a value to the Order property of the [Column] attribute as I read that it's required for composite keys.</li>
  <li>I've only mapped the columns I need to use, as the table has about 10 times more columns than are relevant to this project.</li>
  </ul>
  
  <p>Still, trying to run the project produces an error:</p>
  
  <blockquote>
    <p>InvalidOperationException: The entity type 'MyProject.Models.Order' requires a primary key to be defined.</p>
  </blockquote>
  
  <p>Is there anything more I can add to make this table work with Entity Framework?</p>
  
  <p><strong>EDIT:</strong> I discovered it works if I define it in the context's <code>OnModelCreating</code> method, like <code>modelBuilder.Entity&lt;Order&gt;().HasKey(ord =&gt; new { ord.OrderNo, ord.OrderLineNo });</code>. However I'd still prefer to do it with attributes alone, if possible.</p>