title: How to work with DTOs in Controller of WebAPI?
body: <p>I'm a bit confused how to use DTOs in my WebAPI controllers. I'm using the Database first concept of Entity Framework. The following Entity Data Model was generated:</p>
  
  <pre><code>//Generated class by EDM:
  public partial class Address
      {
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
          public Address()
          {
              this.Member = new HashSet&lt;Member&gt;();
          }
  
          public int Id { get; set; }
          public string Street { get; set; }
          public Nullable&lt;short&gt; Street_Number { get; set; }
          public Nullable&lt;decimal&gt; Zip { get; set; }
          public string City { get; set; }
          public string Country { get; set; }
  
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
          public virtual ICollection&lt;Member&gt; Member { get; set; }
      }
  </code></pre>
  
  <p>To use Data Annotations I need to define a <code>AddressDTO</code> because every time when I need to modify the EDM the data model will generated again and the Data Annotations are lost.</p>
  
  <p>So next, I defined the <code>AddressDTO</code>:</p>
  
  <pre><code>public class AddressDTO
      {
          public int Id { get; set; }
          [Required]
          [StringLength(100,ErrorMessage="The max. length of the street name is 100 characters.")]
          public string Street { get; set; }
          public Nullable&lt;short&gt; Street_Number { get; set; }
          [Required]
          public Nullable&lt;decimal&gt; Zip { get; set; }
          [Required]
          [RegularExpression(@"[a-z,A-Z]",ErrorMessage="Only characters are allowed.")]
          public string City { get; set; }
          [Required]
          public string Country { get; set; }
      }
  </code></pre>
  
  <p>And the controller looks like the code below:</p>
  
  <pre><code>    [RoutePrefix("api/address")]
      public class AddressController : ApiController
      {
          private PersonEntities db = new PersonEntities();
  
          // GET: api/Address
          [HttpGet]
          [ResponseType(typeof(AddressDTO))]
          public async Task&lt;IHttpActionResult&gt; GetAll()
          {
              var addressList = await db.Address.ToListAsync();
  
              return Ok(addressList);
          }
       }
  </code></pre>
  
  <p>When I'm starting the RestAPI to display the result in browser, I always get the following json result:</p>
  
  <pre><code>[
      {
          "Member": [ ],
          "Id": 1,
          "Street": "Example Street",
          "Street_Number": 1,
          "Zip": 12345.0,
          "City": "New York",
          "Country": "USA" 
      },...
  ]
  </code></pre>
  
  <p>But I need the following desired result:</p>
  
  <pre><code>[
          {
              "Street": "Example Street",
              "Street_Number": 1,
              "Zip": 12345.0,
              "City": "New York",
              "Country": "USA" 
          },...
      ]
  </code></pre>
  
  <p>Has anyone an idea how could I resolve that?</p>