title: Web API - Entity Framework is trying to ruin my life
body: <p>I have a simple database with five tables bound together by relationships. I've built OData apps before and like the model, but wanted to make the leap to Web API. The problem is - when I make my call to the ProductType table for example - it returns EVERY record in every table! What gives?</p>
  
  <p>My goal is to call a ProductType for example like this:
  url/api/ProductType
  (and the usual getbyId, delete, etc)</p>
  
  <p>I'm using the repository pattern, and following these steps:
  1.  Create a Entity Framework model with the tables I want to display. Yawn.
  2.  Add an interface in Models, call it IProductTypeRepository, with the behaviors I want to see.
  3.  Add a model class (ProductModel.cs) that inherits from this interface 
  4.  Make sure Global.asax has either default api setup or there’s a App_Start\Routeconfig and WebApiConfig class.
  5.  Surgery on the ValuesController class.</p>
  
  <p>Maybe this is in ignorance of how WebAPI is supposed to work with relationships. Perhaps the goal is to do one call to a service - and get EVERY record across multiple tables (for example, Product and ProductDetails). If so, I think the documentation could have been a LOT clearer on this point!</p>
  
  <p>============================CODE FOLLOWS!================================================
  I’m going to skip the EF auto-generated code obviously – but suffice to say it contains entities and joins for five tables – ProductType, Location, Process, Step, and StepType. ProductType is a very simple table with an ID field and a description and a part # field (MaterialMasterId). </p>
  
  <p>For IProductTypeRepository, here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Perceptive.Services.Models
  {
      interface IProductTypeRepository
      {
          IEnumerable&lt;ProductType&gt; GetAll();
          ProductType GetById(int ProductTypeID);
          int Update(ProductType producttype);
          ProductType Add(ProductType producttype);
          void Delete(int ProductTypeId);
      }
  }
  </code></pre>
  
  <p>For ProducTTypeModel:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;</p>
  
  <pre><code>namespace Perceptive.Services.Models
  {
      public class ProductTypeModel : IProductTypeRepository
      {
  
          private PCSEntities1 context = new PCSEntities1();
  
          public IEnumerable&lt;ProductType&gt; GetAll()
          {
              return context.ProductTypes;
          }
  
          public ProductType GetById(int ProductTypeId)
          {
              IQueryable&lt;ProductType&gt; producttypes = context.ProductTypes.Where(a =&gt; a.ProductTypeID == ProductTypeId);
              return producttypes.FirstOrDefault();
          }
  
          public int Update(ProductType producttype)
          {
              ProductType updateProductType = context.ProductTypes.FirstOrDefault(c =&gt; c.ProductTypeID == producttype.ProductTypeID);
              updateProductType.Description = producttype.Description.Trim();
              updateProductType.MaterialMasterID = producttype.MaterialMasterID;
              return context.SaveChanges();
          }
  
          public ProductType Add(ProductType producttype)
          {
              var addedProductType = context.ProductTypes.Add(producttype);
              context.SaveChanges();
              return addedProductType;
          }
  
          public void Delete(int producttypeid)
          {
              ProductType producttype = context.ProductTypes.FirstOrDefault(a =&gt; a.ProductTypeID == producttypeid);
              context.ProductTypes.Remove(producttype);
              context.SaveChanges();
          }
  
  
  
  
  
      }
  }
  </code></pre>
  
  <p>… and lastly, my Controller – ProductTypeController.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  //dh added
  using Perceptive.Services.Models;
  
  namespace Perceptive.Services.Controllers
  {
      public class ProductTypeController : ApiController
      {
          IProductTypeRepository repository = new ProductTypeModel();
  
          // GET /api/Producttype
          [HttpGet]
          public IEnumerable&lt;ProductType&gt; Get()
          {
              return repository.GetAll();
          }
  
          // GET /api/Producttype/5
          [HttpGet]
          public ProductType Get(int id)
          {
              ProductType producttype = repository.GetById(id);
              return producttype;
          }
  
          // POST /api/ProducTtype
          [HttpPost]
          public void PostProductType(ProductType producttype)
          {
              repository.Add(producttype);
          }
  
          // PUT /api/ProductType
          // DH seems scanty here. Where's the repository put?
          [HttpPut]
          public void PutProductType(ProductType producttype)
          {
              if (repository.Update(producttype) == 0)
              {
                  throw new HttpResponseException(HttpStatusCode.NotFound);
              }
         }
  
          //DELETE /api/ProductType/5
          [HttpDelete]
          public void Delete (int id)
          {
              repository.Delete(id);
          }
      }
  }
  </code></pre>