Unable to scaffold “datetime” type columns from MySQL in .net core 3.1

A similar issue has been posted in the past about scaffolding in .net core 2 but that was on a date column and no solution has been posted to date.

There’s no specific table as any table containing a datetime column type throws the following error from the console:

Could not find type mapping for column 'database.table.columnname' with data type 'datetime'. Skipping column.

This answer to a question about reading datetime values and a MySQL quirk with 0000-00-00 00:00:00 values made me wonder if changing the connection string format would help. So I added the ConvertZeroDateTime=true to the connection string in the scaffold command:

Scaffold-DbContext "server=localhost;port=3306;user=root;password=notherealpassword;database=sodb;ConvertZeroDateTime=true;" MySql.Data.EntityFrameworkCore -OutputDir Model -f

I still have the same error, does anyone have any suggestions? I have 380+ tables in this database so an automated solution would be most helpful.

Answer

I had the same problem and now I solved it.

I can’t be assured if my solution works to your case but I think this solution would work to your case.

  1. Remove all packages related to Entity Framework.
  2. Install the following packages the version is 5.0.4

enter image description here

  1. Open Package Manager Console window. Now we will install Pomelo packages it depends on .Net standard.
  2. Enter the command “Install-Package Pomelo.EntityFrameworkCore.MySql -Version 5.0.0-alpha.2”
  3. Enter the command “Install-Package Pomelo.EntityFrameworkCore.MySql.NetTopologySuite -Version 5.0.0-alpha.2”

The commands try to install the following packages.

enter image description here

  1. Now Enter to Package Manager Console window the following command. Scaffold-DbContext “server=localhost;port=3306;uid=root;pwd=notherealpassword;database=sodb; “Pomelo.EntityFrameworkCore.MySql” -OutputDir Models -f

  2. The result of command is as below. enter image description here

I hope this post helps you.