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.
There is an entity, which contains Hashtable property:
public class Record
{
...
public Hashtable data { get; set; }
}
With ADO.NET, the BinaryFormatter was used to convert this data property to the BLOB, and vice versa:
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);
}
Now I need to tell EF how it should store and retrieve that property.
What have I tried
I could store one more property in the entity:
public class Record
{
public byte[] dataRaw { get; set; }
[NotMapped]
public Hashtable data {
get {/*deserialize dataRaw */ }
set { /*Serialize to dataRaw*/}
}
}
But this solution is prone to errors, and special workflow with that property must be followed.
P.S. Actually this question is not about the Hashtable only, but about every custom class which must be stored and retrived in a special way.