title: Passing an Entity Framework Context object to WCF
body: <p>I need to pass a Context object from EF into a WCF method.</p>
  
  <p>Normally, I create the Context object in the WCF method and dispose of it right before the end of the method call which works just fine for most of my methods.</p>
  
  <p>However, I need to pass the Context object (specifically the DBContext) over from the MVC controller to my specific WCF method because I have caching enabled for some lookup tables. I need this specific Context object passed over (the one I set in the Application_Start method of the Global.asax file) rather than what I do in the sentence above because I use this specific object for the SqlDependency. If I try and create the DBContext object brand new, I can't use the SqlDependency becuase I will get an error informing me that the SqlDependency needs to be enabled before the database call.</p>
  
  <p>The problem is that I'm getting the following error (shortened for brevity) when I try and start my WCF Test Client tool which I know has something to do with not properly declaring a KnownType attribute (ie the DBContext object). Note that the WCF project compiles just fine. I need some help with this specific part since I have never used a KnownType in my WCF service. They have all been simple types (int, string, etc).</p>
  
  <blockquote>
    <p>Error: Cannot obtain Metadata from <a href="http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex" rel="nofollow">http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex</a></p>
    
    <p>If this is a Windows (R) Communication Foundation service to which you
    have access, please check that you have enabled metadata publishing at
    the specified address.  For help enabling metadata publishing, please
    refer to the MSDN documentation at
    <a href="http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata" rel="nofollow">http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata</a> Exchange</p>
    
    <p>Error    URI:
    <a href="http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex" rel="nofollow">http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex</a>
    Metadata contains a reference that cannot be resolved:</p>
  </blockquote>
  
  <p>I have the following OperationContract code in my WCF service:</p>
  
  <pre><code>[OperationContract]
          IEnumerable&lt;Category&gt; GetCategories(YeagerTechEntities DbContext);
  </code></pre>
  
  <p>I have the following DataContract code in my WCF service: </p>
  
  <pre><code>namespace YeagerTechModel
  {
      [Serializable]
      [DataContract(IsReference = true)]
      [KnownType(typeof(YeagerTechEntities))]
      public partial class Category
      {
          public Category()
          {
              this.Projects = new HashSet&lt;Project&gt;();
          }
  
          [DataMember]
          public short CategoryID { get; set; }
          [DataMember]
          public string Description { get; set; }
  
          [DataMember]
          public virtual ICollection&lt;Project&gt; Projects { get; set; }
      }
  
  }
  </code></pre>
  
  <p>Finally, the following is my WCF method:</p>
  
  <pre><code>public IEnumerable&lt;YeagerTechModel.Category&gt; GetCategories(YeagerTechEntities DbContext)
          {
              //YeagerTechEntities DbContext = new YeagerTechEntities();
  
              DbContext.Configuration.ProxyCreationEnabled = false;
  
              IEnumerable&lt;YeagerTechModel.Category&gt; category = DbContext.Categories.Where(p =&gt; p.CategoryID &gt; 0).AsCached("Categories").ToList();
              //IEnumerable&lt;YeagerTechModel.Category&gt; category = DbContext.Categories.Where(p =&gt; p.CategoryID &gt; 0);
  
              CloseConnection(DbContext);
  
              return category;
          }
  </code></pre>