title: Converting an entity / model back to DataTable
body: <p>For legacy reasons, I am having to call ASMX web services which give me a <code>DataTable</code> instead of an entity, and then in turn pass back a data table to the web service. I'm using the term <strong>entity</strong> here, but there is no ORM as the entire data layer (CRUD) is done by these web services. I simply call a web method which gives me a data table, convert this data table to an entity, update the entity, convert back to a data table and then finally pass this data table to a web method.</p>
  
  <p>At the moment I'm using AutoMapper to convert from the data table's row to the entity with this <code>Profile</code>:</p>
  
  <pre><code>Mapper.CreateMap&lt;DataRow, MyEntity&gt;()
      .ForMember(d =&gt; d.Property, o =&gt; o.MapFrom(s =&gt; s["property_column"]))
      // and so on
  </code></pre>
  
  <p>Which works perfectly fine, however <strong>what I wanted to do</strong> to get the entity back to the data table row was to use this <code>Profile</code> (which unfortunately doesn't work):</p>
  
  <pre><code>Mapper.CreateMap&lt;MyEntity, DataRow&gt;()
      .ForMember(d =&gt; d["property_column"], o =&gt; o.MapFrom(s =&gt; s.Property))
      // and so on
  </code></pre>
  
  <p>So, because AutoMapper can't do this I <strong>think</strong> I'll have to use reflection - but this brings me to my other problem. My entity property names don't match my data row's column names. So I can't simply <code>DataRow[PropertyInfo.Name] = value</code>. </p>
  
  <p>I was thinking of using a <code>EntityTypeConfiguration</code> class (for each entity) to map the property to the target column, but this would mean I'd need references to Entity Framework inside my project - something I'd prefer to avoid as it won't be used. My second (and preferred) thought was to create custom attributes for my entities which contain the target column name and in my <code>ConvertToDataTable&lt;T&gt;</code> class reflect on these attributes to hydrate the data row.</p>
  
  <p>Does anyone have a better way of achieving this, something I've likely overlooked?</p>