title: Why does a simple entity data model relationship return null instead of a reference to an object?
body: <p>I have 2 simple tables and a foreign key defined in SQL Server Express:</p>
  
  <hr>
  
  <p>Product </p>
  
  <ul>
  <li>ProductID [auto-inc]</li>
  <li>Name</li>
  <li>CompanyID [not-null]</li>
  </ul>
  
  <p>Company</p>
  
  <ul>
  <li>CompanyID [auto-inc]</li>
  <li>Name</li>
  </ul>
  
  <p>FK_Product_Company</p>
  
  <ul>
  <li>Product.CompanyID = Company.CompanyID</li>
  </ul>
  
  <hr>
  
  <p>I created an ADO.NET Entity Data Model and added all the tables to it. The .edmx file shows the 1-to-many relationship in the designer. I manually populated the database with data that would make sure each product has a company. Whenever I try to access a product's company, however, it always returns null instead of an instance of a company.</p>
  
  <p>For example, running the following code in C#, returns null:</p>
  
  <pre><code>var _db = new MyDBEntities();
  var product = (from p in _db.Product
                 where p.ProductID == 3
                 select p).First();
  product.Company // == null
  </code></pre>
  
  <p>Are there any steps I'm missing to get this to work?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <h2>Things I have tried:</h2>
  
  <hr>
  
  <p>Running the following SQL, returns the company record correctly.</p>
  
  <pre><code>SELECT Company.*
  FROM Product
    LEFT JOIN Company ON (Product.CompanyID = Company.CompanyID)
  WHERE Product.ProductID = 3
  </code></pre>
  
  <hr>
  
  <p>The next thing I would do to debug this issue, would be to run the following code:</p>
  
  <pre><code>var _db = new MyDBEntities();
  var product = (from p in _db.Product
                 where p.ProductID == 3
                 select p).First();
  var company = (from c in _db.Company
                 where c.CompanyID == product.CompanyID
                 select c).First();
  </code></pre>
  
  <p>However, that does not compile because the field Product.CompanyID is hidden by the ORM generator and I don't see an option in the designer to add it.</p>
  
  <hr>
  
  <p>The next best thing to run I guess is the following, and it did return a company:</p>
  
  <pre><code>var _db = new MyDBEntities();
  var company = (from c in _db.Company
                 where c.CompanyID == 2
                 select c).First();
  </code></pre>
  
  <p>Just to be clear, Product with ID 3 corresponds to Company with ID 2.</p>
  
  <hr>
  
  <p>I tried creating the exact same relationship using <em>LINQ to SQL Classes</em> and it works fine.</p>
  
  <p>Of course instead of using the DB Entities class, I am using the Data Context class.</p>