title: Retrieving the value in an association table in Entity Framework code first
body: <p>I am using <code>EF 4.1 code first</code> and I am struggling with the association entity and getting the value that was set in the association table.  I tried to follow the post on: <a href="https://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table/7053393#7053393">Create code first, many to many, with additional fields in association table</a>.</p>
  
  <p>My tables are as follows (all are in plural form):</p>
  
  <p>Table: Products</p>
  
  <pre><code>Id int
  Name varchar(50)
  </code></pre>
  
  <p>Table: Specifications</p>
  
  <pre><code>Id int
  Name varchar(50)
  </code></pre>
  
  <p>Table: ProductSpecifications</p>
  
  <pre><code>ProductId int
  SpecificationId int
  SpecificationValue varchar(50)
  </code></pre>
  
  <p>My related classes:</p>
  
  <pre><code>public class Product : IEntity
  {
       public int Id { get; set; }
       public string Name { get; set; }
       public virtual ICollection&lt;ProductSpecification&gt; ProductSpecifications { get; set; }
  }
  
  public class Specification : IEntity
  {
       public int Id { get; set; }
       public string Name { get; set; }
       public virtual ICollection&lt;ProductSpecification&gt; ProductSpecifications { get; set; }
  }
  
  public class ProductSpecification
  {
       public int ProductId { get; set; }
       public virtual Product Product { get; set; }
       public int SpecificationId { get; set; }
       public virtual Specification Specification { get; set; }
       public string SpecificationValue { get; set; }
  }
  </code></pre>
  
  <p>My context class:</p>
  
  <pre><code>public class MyContext : DbContext
  {
       public DbSet&lt;Product&gt; Products { get; set; }
       public DbSet&lt;Specification&gt; Specifications { get; set; }
       public DbSet&lt;ProductSpecification&gt; ProductSpecifications { get; set; }
  
       protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
       {
       }
  }
  </code></pre>
  
  <p>My repository method where I do my call (not sure if it is correct):</p>
  
  <pre><code>public class ProductRepository : IProductRepository
  {
       MyContext db = new MyContext();
  
       public Product GetById(int id)
       {
            var product = db.Products
                 .Where(x =&gt; x.Id == id)
                 .Select(p =&gt; new
                 {
                      Product = p,
                      Specifications = p.ProductSpecifications.Select(s =&gt; s.Specification)
                 })
                 .SingleOrDefault();
  
            return null;  // It returns null because I don't know how to return a Product object?
       }
  }
  </code></pre>
  
  <p>Here is the error that I am getting back:</p>
  
  <pre><code>One or more validation errors were detected during model generation:
  
  System.Data.Edm.EdmEntityType: : EntityType 'ProductSpecification' has no key defined. Define the key for this EntityType.
   System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ProductSpecifications� is based on type �ProductSpecification� that has no keys defined.
  </code></pre>
  
  <p>What does it mean that no keys are defined?  Won't the ProductId and SpecificationId map to Id of Product and Id of Specification respectively?</p>
  
  <p>How would I return a single product with the all the specifications for it?</p>