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

MySql with ASP.NET Core

  1. Create the project: dotnet new console
  2. Add mysql package: dotnet add package MySql.Data
  3.  Optional: Add an ORM: dotnet add package Dapper
  4. Start vscode: code .
  5. Connect and and query
  6. Run the code: dotnet run

     static void Main(string[] args)
        {
            MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
            conn_string.Server = "SERVERAdress";
            conn_string.UserID = "USERid";
            conn_string.Password = "Pws";
            conn_string.Database = "Db";

            MySqlConnection connection = new MySqlConnection
            {
                ConnectionString = conn_string.ToString()
            };
            connection.Open();

            MySqlCommand command = new MySqlCommand("SELECT * FROM Notes;", connection);

            using (MySqlDataReader reader = command.ExecuteReader())
            {

                while (reader.Read())
                {
                    string row = $"{reader["NoteId"]}\t\t{reader["NoteTitle"]}\t\t{reader["NoteText"]}";
                    System.Console.WriteLine(row);
                }
            }

            connection.Close();

            System.Console.ReadKey();
        }

#asp-net-core #mysql #mysql-with-net-core

Floor, Ceiling and Rounding with MySQL

I was trying to get the average solution time of issues in Redmine per tracker type. In order to get the average time, I first selected creation time of issue and extracted it from the date of solution of the issue (this doesn’t mean it is closed, it is to say status is updated with the id =X),  after that I listed the average of each day.

ROUND(AVG(DATEDIFF(journals.created_on,issues.created_on))) AverageSolutionTimeRounded,

Some mathematical functions of MySQL could be a useful. Here they are: Continue reading

#ceiling, #floor, #mysql, #rounding