Save Collection As JSON with Entity Framework Save Collection As JSON with Entity Framework json json

Save Collection As JSON with Entity Framework


The problem with the accepted answer is that any changes to the contents of the list (adding, modifying or deleting entries) won't be tracked by EF.

Here is my solution, inspired by this excellent blog post.

This class takes care of serializing and deserializing so that the collection can be stored in a single column on the parent model:

[ComplexType]public class DateTimeCollection : Collection<DateTime>{    public void AddRange(IEnumerable<DateTime> collection)    {        foreach (var item in collection)        {            Add(item);        }    }    [Column("Times")]    public string Serialized    {        get { return JsonConvert.SerializeObject(this); }        private set        {            if (string.IsNullOrEmpty(value))            {                Clear();                return;            }            var items = JsonConvert.DeserializeObject<DateTime[]>(value);            Clear();            AddRange(items);        }    }}

That's it! You can now use this new collection on your parent class exactly as you'd expect. Changes to the contents of the collection will be tracked.

public class Company{    // ...    public DateTimeCollection Times { get; set; }}


The following should work (I used Json.Net, but you can change it to any other serializer):

public class Company{    public int Id {get;set;}    public string Name {get;set;}    [NotMapped]    public List<DateTime> Times {get;set;}    [Column("Times")]    public string TimesSerialized    {        get        {            return JsonConvert.SerializeObject(Times);        }        set        {            Times = string.IsNullOrEmpty(value)                    ? new List<DateTime>()                    : JsonConvert.DeserializeObject<List<DateTime>>(value);        }    }}

You can also make TimesSerialized private if you map it manually.