Pomelo.EntityFrameworkCore.MySql: Invalid default value with CLR type DateTime

Hi there

Sorry if this is another silly question.

I have the following model

public class TankLevel{
    public int id { get; set; }
    public DateTime dateLogged { get; set; } = DateTime.Now;
    public int level { get; set; }
}

and the following in my OnModelCreating

modelBuilder.Entity<TankLevel>()
     .Property(l => l.dateLogged)
     .HasDefaultValueSql("CURRENT_TIMESTAMP");

Unfortunately, when I try to update the database, I get the error Invalid default for dateLogged.

Am I doing something silly?

Thanks in advance

Stuart

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (4 by maintainers)

Most upvoted comments

If your mysql version is >= 5.6, you should use CURRENT_TIMESTAMP(6) instead of CURRENT_TIMESTAMP.

See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

In Pomelo:

MySQL CLR Type Stored Type
>=5.6 DateTime datetime(6)
>=5.6 DateTimeOffset datetime(6)
>=5.6 TimeSpan time(6)
< 5.6 DateTime datetime
< 5.6 DateTimeOffset datetime
< 5.6 TimeSpan time

BTW, you needn’t init the Time value in c#. Just remove = DateTime.Now; in your model.