title: Exposing Entity Framework over WCF Service
body: <p>I need some help here! I'm trying to create my first application using Entity Framework, WCF, MVVM and WPF technologies. I'm new to all of them. So I create a model for my database using Entity Framework. Then I create my WCF service class. I have read close to 50 articles about EF and WCF services and I'm all about confusion right now. I know that I'm not supposed to expose my model directly. Currently I'm using this code as service contract:</p>
  
  <pre><code>namespace Ohmio.DataService
  {
      [ServiceContract]
  
      public class OhmioService
      {
          [OperationContract]
          public IEnumerable&lt;vw_Pedidos&gt; ListarPedidos(string IDComprobante, bool bEntregados, int Numero = -1, int IDCliente = -1)            
          {
              using (var context = new OhmioTestNet())
              {
                  string sqlString="SELECT VALUE cs FROM OhmioTestNet.vw_Pedidos AS cs WHERE cs.ID_Comprobante=='" + IDComprobante + "' AND ";
                  if (Numero != -1) sqlString += "cs.Numero=="+ Numero.ToString() +" AND ";
                  if (IDCliente != -1) sqlString += "cs.ID_Cliente=="+ IDCliente.ToString()+" AND ";
                  if (!bEntregados) sqlString += "cs.Entregado==false AND ";                 
                  sqlString =sqlString.Substring(0,sqlString.Length-4);
  
                  ObjectQuery&lt;vw_Pedidos&gt; Pedidos = context.CreateQuery&lt;vw_Pedidos&gt;(sqlString);                
                  var result = Pedidos.ToList();
                  result.ForEach(e =&gt; context.Detach(e));
                  return result;
              }
          }
  
          [OperationContract]
          public IEnumerable&lt;Clientes&gt; GetClientes()
          {
              using (var context = new OhmioTestNet())
              {
                  var result = context.Clientes.Where(f =&gt; f.Activo == true).ToList();
                  result.ForEach(e =&gt; context.Detach(e));
                  return result;
              }
          }
      }
  }
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li><p>The problem with it is that don't suppose to expose my data model directly, right? So here I'm returning detached entity of class <code>vw_Pedidos</code>. Is this a bad practice?</p></li>
  <li><p>I have read a lot about DTO's and POCO's object, which should I use instead of the above code?</p></li>
  <li><p>If I use DTO or POCO to transfer over WCF, do I have to manually create a DTO for each database object? Do I have to manually create all the properties (fields) for each DB object?</p></li>
  <li><p>If I add a new field to a database object and need to show it on client, do I have to update my EF model, add the new field manually to the DTO or POCO object? This sounds like a maintenance nightmare!</p></li>
  <li><p>Is using EntitiesToDTOs a good option for automatic create DTOs based on EF objects?</p></li>
  <li><p>Can I use DTO to update data back to database?</p></li>
  </ol>
  
  <p>Sorry for the multiple questions. Please help me out here! I'm searching for a pattern that lead me to an easy to maintain, concern separated code. Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Following TomTom suggestions, i read a lot about POCOs, EF and Linq to Entities. After that i re-write my whole app. I divide my code on 5 projects:</p>
  
  <p>1)DataLayer</p>
  
  <p>2)ModelLayer</p>
  
  <p>3)BusinessLyer</p>
  
  <p>4)WCF Service Layer</p>
  
  <p>5)WPF Client</p>
  
  <p>For comunications on layers 1 to 5 I use POCO classes mapped to Entity Framework using POCO T4 Template. To comunicate layer 4 and 5 over WCF I want to use a custom class to hide some field from client (I don't want to expose the whole POCO class) so I re-write filter function using linq to entities and projection to IEnumerable:</p>
  
  <pre><code>public IEnumerable&lt;PedidosList&gt; Pedidos_Listar(string sComprobante, Clientes MyCliente = null, DateTime? dDesde = null, DateTime? dHasta = null, bool bCumplidos = false)
              {           
                  using (var context = new OhmioEntities())
                  {
                      IEnumerable&lt;PedidosList&gt; query =
                          from Pedidos in context.Pedidos
                          join Clientes in context.Clientes on Pedidos.ID_Cliente equals Clientes.ID_Cliente
                          where Pedidos.ID_Comprobante == sComprobante                    
                          select new PedidosList {ID_Pedido = Pedidos.ID_Pedido, Fecha=Pedidos.Fecha, Aprobado=Pedidos.Aprobado, Bruto=Pedidos.Bruto, Cliente=Clientes.RazonFantasia, 
                              FechaEntrega=Pedidos.FechaEntrega, Neto=Pedidos.Neto, Numero=Pedidos.Numero, Observaciones=Pedidos.Observaciones, Entregado=Pedidos.Entregado, ID_Cliente=Pedidos.ID_Cliente };
  
                      if (MyCliente != null) query = query.Where(i =&gt; i.ID_Cliente == MyCliente.ID_Cliente);
                      if (MyCliente != null) query = query.Where(i =&gt; i.ID_Cliente == MyCliente.ID_Cliente);
                      if (dDesde != null &amp;&amp; dHasta != null) query = query.Where(i =&gt; i.Fecha &gt;= dDesde &amp;&amp; i.Fecha &lt;= dHasta);
                      if (bCumplidos == false) query = query.Where(i =&gt; i.Entregado == false);                
                      return query.ToList();
                  }
              }
  </code></pre>
  
  <p>Is this the best way to do this? Can i make the projection AFTER the filter? Thanks!</p>