title: WCF return entity framework classes that have WCF atrributes on them
body: <p>I recently talked to a programmer who has never used the entity framework, however used WCF services often. So I decided to create some models using the entity framework code first approach. I thought I added the proper attributes to the classes so that data can be returned and so on. However, when testing the service in visual studio I get this exception. </p>
  
  <p>--Update--
  Inside of the classes removing the virtual keyword makes everything work. However in my returned classes a quiz no longer has any values inside of the terms property.</p>
  
  <p>--Update--
  Found a way that isn't what I really want but it works. Created two new classes, DB_Quiz, DB_Term, they're identical to the entity framework classes aside from virtual keywords. 
  Then during the method call I do something like this.</p>
  
  <pre><code>var query = (from search in db.Quizes select new DB_Quiz() {
                  ID =search.ID, Title = search.Title
  
              });
  
              var obj = query.FirstOrDefault();
  
  
              var termQuery = (from search in db.Terms where search.Quiz.ID == obj.ID select new DB_Term() {
                  ID = search.ID, Phrase = search.Phrase, Definition = search.Definition
              }).ToList();
  
              obj.Terms = termQuery;
  </code></pre>
  
  <p>--Update-- 
  Found a good solution however relating to this question it does not answer it correctly. Same as update above however the linq query is slightly more advanced.</p>
  
  <pre><code>var query = (from search in db.Quizes
                           select new DB_Quiz()
                           {
                               ID = search.ID,
                               Title = search.Title,
                               Terms =
                               (from inner in search.Terms select new DB_Term() {
                                   ID = inner.ID, Phrase = inner.Phrase, Definition = inner.Phrase
                               }).ToList()
                           });
  
              return query.ToList();
  </code></pre>
  
  <p>An error occurred while receiving the HTTP response to <a href="http://localhost:61149/DataWorker.svc" rel="nofollow noreferrer">http://localhost:61149/DataWorker.svc</a>. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.</p>
  
  <p>I just want to build my backend to use the entity framework because of what I have seen it makes working with databases so easy. Someone please help me understand this problem. I would really like to stick to WCF services because I am using the UWP as my client application. From what I have seen the entity framework doesn't work well with WCF services.</p>
  
  <p>My classes:
  ContextClass -</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.Web;
  
  namespace QuizletBackend.Models
  {
      [DataContract]
      public class QuizContext : DbContext
      {
          public QuizContext() : base("name=LocalServer")
          {
  
          }
  
          [DataMember]
          public DbSet&lt;Quiz&gt; Quizes { get; set; }
          [DataMember]
          public DbSet&lt;Term&gt; Terms { get; set; }
      }
  }
  </code></pre>
  
  <p>Quiz class -</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.Web;
  
  namespace QuizletBackend.Models
  {
      [DataContract]
      public class Quiz
      {
          [Key]
          [DataMember]
          public int ID { get; set; }
  
          [DataMember]
          [Index(IsUnique = true)]
          [StringLength(150, ErrorMessage = "Title cannot exceed 150 characteres.")]
          public String Title { get; set; }
          [DataMember]
          public virtual List&lt;Term&gt; Terms { get; set; } = new List&lt;Term&gt;();
      }
  }
  </code></pre>
  
  <p>Term class -</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.Web;
  
  namespace QuizletBackend.Models
  {
      [DataContract]
      public class Term
      {
          [DataMember]
          [Key]
          public int ID { get; set; }
          [DataMember]
          public String Phrase { get; set; }
          [DataMember]
          public String Definition { get; set; }
          [DataMember]
          public virtual Quiz Quiz { get; set; }
      }
  }
  
  Server stack trace: 
     at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
     at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
     at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
     at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
     at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
  
  Exception rethrown at [0]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
     at IDataWorker.DoWork()
     at DataWorkerClient.DoWork()
  
  Inner Exception:
  The underlying connection was closed: An unexpected error occurred on a receive.
     at System.Net.HttpWebRequest.GetResponse()
     at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
  
  Inner Exception:
  Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
     at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
     at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
     at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
  
  Inner Exception:
  An existing connection was forcibly closed by the remote host
     at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
     at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
  </code></pre>