From 586dda37483119aa60ea8dd2c9d720f43687fc99 Mon Sep 17 00:00:00 2001 From: Laurents Meyer Date: Thu, 24 Oct 2019 21:08:11 +0200 Subject: [PATCH] Correctly implement `CURRENT_TIMESTAMP` with `ON UPDATE` clauses. Introduce a workaround for the missing EF Core handling of `ValueGenerated.OnUpdate`. Fixes #877 --- .../Internal/MySqlDatabaseModelFactory.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs b/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs index 66220b2bd..c885f7490 100644 --- a/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs +++ b/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs @@ -212,13 +212,30 @@ private void GetColumns( } else if (extra.IndexOf("on update", StringComparison.Ordinal) >= 0) { - if (defaultValue != null && extra.IndexOf(defaultValue, StringComparison.Ordinal) > 0) + if (defaultValue != null + && (extra.IndexOf(defaultValue, StringComparison.Ordinal) > 0 + || string.Equals(dataType, "timestamp", StringComparison.OrdinalIgnoreCase) + && extra.IndexOf("CURRENT_TIMESTAMP", StringComparison.Ordinal) > 0)) { valueGenerated = ValueGenerated.OnAddOrUpdate; } else { - valueGenerated = ValueGenerated.OnUpdate; + // BUG: EF Core does not handle code generation for `OnUpdate`. + // Instead, it just generates an empty method call ".()". + // Tracked by: https://github.com/aspnet/EntityFrameworkCore/issues/18579 + // + // As a partial workaround, use `OnAddOrUpdate`, if a default value + // has been specified. + + if (defaultValue != null) + { + valueGenerated = ValueGenerated.OnAddOrUpdate; + } + else + { + valueGenerated = ValueGenerated.OnUpdate; + } } } else