title: Calling a stored procedure in Entity Framework 6 Repository Pattern
body: <p>I am familiar with Entity Framework (6) on a slightly more than basic level. I built a system based on a pattern I got from a book. I effectively makes all db calls nicely generic. It did not however address calling stored procedures and I have some that I need. I'm exactly sure how to call them based on this pattern.  </p>
  
  <p>Here is how it is established for Fetches:</p>
  
  <pre><code>public class EFRepository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      public EFRepository(DbContext dbContext)
      {
          if (dbContext == null)
              throw new ArgumentNullException("dbContext");
  
          DbContext = dbContext;
          DbSet = DbContext.Set&lt;T&gt;();
      }
  
      protected DbContext DbContext { get; set; }
      protected DbSet&lt;T&gt; DbSet { get; set; }
  
      public virtual IQueryable&lt;T&gt; GetAll()
      {
          return DbSet;
      }
  
      public virtual IQueryable&lt;T&gt; GetTop50()
      {
          return DbSet.Take(50);
      }
  
      public virtual T GetById(int id)
      {
          return DbSet.Find(id);
      }
  }
  </code></pre>
  
  <p>So, I guess the question is, how would I make the call, passing in the name of the stored procedure and the parameter(s)? Is the result still a <code>DbSet</code>?  The stored procedure <strong><em>IS</em></strong> established in EF.  </p>
  
  <pre><code>public virtual ObjectResult&lt;PropertiesContactIsInvolvedIn_Result&gt; PropertiesContactIsInvolvedIn(Nullable&lt;int&gt; contactID)
  {
      var contactIDParameter = contactID.HasValue ?
              new ObjectParameter("ContactID", contactID) :
              new ObjectParameter("ContactID", typeof(int));
  
      return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction&lt;PropertiesContactIsInvolvedIn_Result&gt;("PropertiesContactIsInvolvedIn", contactIDParameter);
  }
  </code></pre>
  
  <p>Additionally, as I said, there is a repository approach involved, and the entities are expressed like this:</p>
  
  <pre><code>public IRepository&lt;PropertiesContactIsInvolvedIn_Result&gt; PropertiesContactIsInvolvedIn { get { return GetStandardRepo&lt;PropertiesContactIsInvolvedIn_Result&gt;(); } }
  
  IRepository&lt;PropertiesContactIsInvolvedIn_Result&gt; PropertiesContactIsInvolvedIn { get; }
  </code></pre>