I have the following entity:
public class SampleClass { public int Id { get; set; } public object Args {get; set; } }
Because Args can be of different types and does not need a request, I want to save it in the database as a json string.
I know the following way to solve my problem:
public class SampleClass { public int Id { get; set; } public object Args { get { return Json.Deserialize(ArgsJson); } set { ArgsJson = Json.Serialize(value); } } public string ArgsJson {get; set; } }
But this is pretty ugly, because it provides information not related to the model, and contains logic that is not related to the model again.
What I would like to do is something like this:
public class SampleClassMapper : EntityTypeConfiguration<SampleClass> { public SampleClassMapper() { this.Property(e => e.Args).MapAs<string>(arg => Json.Serialize(arg), str => Json.Deserialize(str)); } }
Is there a cool way to do this?
(I am using .Net 4.0 with EntityFramework 5 and Sql Server 2008, if that helps)
source share