Search code examples
jsonwinformsentity-frameworkef-code-firstcode-first

Is it possible to use JSON file as backend storage in Code First Entity Framework?


First of all I am talking about WinForms projects on Win10 and not ASP.NET in case somebody feels inclined to answer for ASP.NET.

Now, I will be using Entity Framework with code-first approach, I have used that in the past a lot with SQL & SQL Lite.

My question is, what if I don't want to use SQL/Lite as backend but use JSON? I noticed a nice WPF application storing all its relational data in a JSON file but I can't seem to find anything regarding using JSON as my backend RDBMS. It won't be a large database, but it is definitely relational data.

I use Entity Framework, Winforms, .NET and C#. I would like to have something like this:

class Top {
   public int ID {get;set;}
   public string Title {get;set;}
   public TagInfo MainTag {get;set;}
   public HashSet<TagInfo> TagCatalog {get;set;}
}

class TagInfo {
   public int ID {get;set;}
   public string TagName {get;set;}
   public Color  TagColor {get;set;}
   public DogInfo Dog {get;set;}
}

class DogInfo {
   public int ID {get;set;}
   public string DogName {get;set;}
   public Color  DogColor {get;set;}
}

What I say is that when I serialize Top I don't want the Top.MainTag to be serialized as the full TagInfo but only its ID because all the tag information is already on the TagCatalog property.


Solution

  • You don't need EF for that. Just use a JSON serializer to save and load a bunch of objects to a file.