2

I´m trying to update a MySql Database field to an automated datetime field using Migrations.

Here is the code:

    public partial class AutomatedDateField : DbMigration
    {
        public override void Up()
    {
        this.AlterColumn("Orders", "Created", c => c.DateTime(nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"));
    }

Using SQL Server i would use GETUTCDATE() in defaultValueSql. What should i use in this case?

paulocinf
  • 21
  • 2
  • https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_utc-date – Steve Greene Jun 11 '15 at 18:13
  • I already tried utc_timespan() as well as utc_date() but i didn't succeed. When i run update-database in PMC i receive the error «You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UTC_DATE()' at line 1» – paulocinf Jun 11 '15 at 23:08
  • I guess the problem is in MySql version. The one i'm working on is 5.5.34 and it doesn't allow initialization values for datetime type. I had to handle that on SaveChanges method by capturing all added entity entries and setting Created to Datetime.UtcNow – paulocinf Jun 12 '15 at 01:42

1 Answers1

0

You can't use function as default value for column in MySQL.

Possible solution can be found here: Can I use a function for a default value in MySql?

Community
  • 1
  • 1