title: EF : The entity type is not part of the model for the current context using stored procedure
body: <p>I'm working with EF5. I've used code first approach. I got an error while using stored procedure. The error is </p>
  
  <pre><code>"The entity type CustomProduct is not part of the model for the current context."
  </code></pre>
  
  <p>In Db there are 3 tables. </p>
  
  <ul>
  <li><p>Product </p>
  
  <ul>
  <li>ProductId</li>
  <li>ProductName</li>
  <li>ProductDesription</li>
  </ul></li>
  <li><p>ProuctVaraint </p>
  
  <ul>
  <li>ProductVaraintId</li>
  <li>ProductId</li>
  <li>ProductVaraintName</li>
  <li>Stock</li>
  <li>Size   </li>
  </ul></li>
  <li><p>ProductPrice</p>
  
  <ul>
  <li>ProductPriceId</li>
  <li>ProudctId</li>
  <li>Price</li>
  </ul></li>
  </ul>
  
  <p>and Each entity has separate class with having all properties. </p>
  
  <ul>
  <li>Product.cs</li>
  <li>ProductVaraint.cs</li>
  <li>ProductPrice.cs</li>
  </ul>
  
  <p>Here is all classes </p>
  
  <pre><code>public class Product
      {
          public int ProductId { get; set; }
  
          public string ProductName { get; set; }
  
          public string ProductDescription { get; set; }
  
      }
  
      public class ProductVaraint
      {
          public int ProductVaraintId { get; set; }
  
          public int ProductId { get; set; }
  
          public string ProdcutVaraintName { get; set; }
  
          public int Stock { get; set; }
  
          public int Size { get; set; }
      }
  
      public class ProductPrice
      {
          public int ProductPriceId { get; set; }
  
          public int ProductId { get; set; }
  
          public decimal Price { get; set; }
      }
  
      public class CustomProduct
      {
          public int ProudctId { get; set; }
  
          public string ProductName { get; set; }
  
          public string ProductDescription { get; set; }
  
          public int Stock { get; set; }
  
          public int Size { get; set; }
  
          public decimal Price { get; set; }
      }
  </code></pre>
  
  <p>Here is stored Procedure</p>
  
  <pre><code>   public IList&lt;CustomProduct&gt; ExecuteSP()
      {
          var context = ((IObjectContextAdapter)(this)).ObjectContext;
          var connection = this.Database.Connection;
  
              //open the connection
              if (connection.State == ConnectionState.Closed)
                  connection.Open();
              //create a command object
              using (var cmd = connection.CreateCommand())
              {
                  //command to execute
                  cmd.CommandText = "GetProducts";
                  cmd.CommandType = CommandType.StoredProcedure;
  
                  var reader = cmd.ExecuteReader();
  
                  var result = context.Translate&lt;CustomProduct&gt;(reader).ToList();
                  for (int i = 0; i &lt; result.Count; i++)
                      result[i] = AttachEntityToContext(result[i]);
                  reader.Close();
                  return result;
              }
  
          }
      }
  
  
  
  public TEntity AttachEntityToContext&lt;TEntity&gt;(TEntity entity) where TEntity : BaseEntity, new()
          {
  
  var alreadyAttached = Set&lt;TEntity&gt;().Local.Where(x =&gt; x.Id == entity.Id).FirstOrDefault();
              if (alreadyAttached == null)
              {
                  Set&lt;TEntity&gt;().Attach(entity);
                  return entity;
              }
              else
              {
                  return alreadyAttached;
              }
          }
  </code></pre>
  
  <p>I've mapped Product.cs,ProductVaraint.cs and ProductPrice.cs with DBContext but not mapped CustomProduct.cs</p>
  
  <p>Now, I've made one stored procedure to return ProductName(from Product),ProductDescription(from product),Stock(from ProductVaraint),Size(from ProductVaraint) and Price(from ProductPrice).</p>
  
  <p>To map this properties, I've a separate class called CustomProudct.cs that contains all those properties which is return by the stored procedure and map this class with store procedure.</p>
  
  <p>There is no separate table for CustomProduct and I don't need to create extra table only for mapping sp result.</p>
  
  <p>I know the causes that there is no separate table of CustomProduct and EF is trying to search in Db for this table and it does not find any table that's why it is throwing exception.</p>
  
  <p>Please anyone suggest me how to do this. Is there any other way to handle this type of situation?</p>