I have Code First class/table and one of the fields has type string/nvarchar. This string is JSON representation of MyClass instance. I'd like to operate in code with MyClass instances only, but store it as a string (JSON) in database. Let's say my table looks like this:
public class Message
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string JsonDefinition { get; set; }
}
I'd prefer to have it like this
public class Message
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
[JSON]
public MyClass JsonDefinition { get; set; }
}
Where JSON is a custom attribute which tells EF to store the field as serialized string of MyClass instance. And at the same time it says EF: "once you pull the entity, replace JsonDefinition string with deserialized instance of MyClass"
Is it possible to achieve with existing EF 4 mechanisms? If so, then how?
Thanks in advance.
EDIT: MyClass could be a Dictionary or any other complex type.