title: Storing and retrieving data as JSON string with Entity Framework Core?
body: <p>I am trying to store an object as JSON. Then, convert it back from JSON string to an object with Entity Framework Core 3 using C#.</p>
  <p>I have the following 2 model classes:</p>
  <pre><code>public class A
  {    
      public int Id { get; set; }
  
      // more properties
  
      // I want to store the data as json.
      public CustomObject Object { get; set; }
  }
  
  public class CustomObject
  {
      public int? Id { get; set; }
      public int[] ItemIds { get; set; }
      public string Value { get; set; }
  }
  </code></pre>
  <p>Then in my <code>DbContext</code> I added the following</p>
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      base.OnModelCreating(modelBuilder);
  
      modelBuilder.Entity&lt;A&gt;.Property(x =&gt; x.Object).HasConversion(
           v =&gt; JsonConvert.SerializeObject(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
           v =&gt; JsonConvert.DeserializeObject&lt;T&gt;(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })
              );
  }
  </code></pre>
  <p>However, when the app run I get the following error</p>
  <blockquote>
  <p>System.InvalidOperationException: 'The property 'Object.ItemIds' could not be mapped, because it is of type 'int[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'</p>
  </blockquote>
  <p>That error occurs when I try to set the entity in a generic class like this</p>
  <pre><code>Repository = context.Set&lt;A&gt;();
  </code></pre>
  <p>How can I correctly convert JSON string to an object and an object back to a JSON string with Entity Framework Core?</p>