JSON Type with MySQL & EF Core

As of MySQL 5.7.8, it has support for Json type. Setting the column type to json would do the job.

  [Column(TypeName = "json")]
  public string Settings{ get; set; }

Or with fluent api:

       modelBuilder.Entity<Blog>(eb =>
        {
            eb.Property(b => b.Settings).HasColumnType("json");
        });

If you send an invalid JSON, MySQL will throw an error of “Invalid JSON text”.
Details on usage are in the official documentation:
https://dev.mysql.com/doc/refman/5.7/en/json.html

In the case of custom objects, Pomelo has a feature as well: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/14

A nive tutorial from scotch.io:
https://scotch.io/tutorials/working-with-json-in-mysql

#json #mysql #mysqlJson #efcore