title: How to deal with Hashtable property of entity in database using EntityFramework
body: <p>I have old project which used ADO.NET to access the persistent store. Currently, I want to migrate it to EF (6.1.3, if it matters), in order to support several DB providers with minimal code duplicate.</p>
  
  <p>There is an entity, which contains <code>Hashtable</code> property:</p>
  
  <pre><code>public class Record
  {
      ...
      public Hashtable data { get; set; }
  }
  </code></pre>
  
  <p>With ADO.NET, the <code>BinaryFormatter</code> was used to convert this <code>data</code> property to the BLOB, and vice versa:</p>
  
  <pre><code>using (MemoryStream stream = new MemoryStream())
  {
      BinaryFormatter formatter = new BinaryFormatter();
      formatter.Serialize(stream, data);
      result = stream.GetBuffer();
  }
  
  //----------
  
  using (MemoryStream serializationStream = new MemoryStream((byte[])value))
  {
      BinaryFormatter formatter = new BinaryFormatter();
      result = (Hashtable)formatter.Deserialize(serializationStream);
  }
  </code></pre>
  
  <p>Now I need to tell EF how it should store and retrieve that property.</p>
  
  <p><strong>What have I tried</strong></p>
  
  <p>I could store one more property in the entity:</p>
  
  <pre><code>public class Record
  {
      public byte[] dataRaw { get; set; }
  
      [NotMapped]
      public Hashtable data {
          get {/*deserialize dataRaw */ }
          set { /*Serialize to dataRaw*/}
      }
  }
  </code></pre>
  
  <p>But this solution is prone to errors, and special workflow with that property must be followed.</p>
  
  <p><strong>P.S.</strong> Actually this question is not about the Hashtable only, but about every custom class which must be stored and retrived in a special way.</p>