diff --git a/src/DslPackage/TextTemplates/EFCore5ModelGenerator.ttinclude b/src/DslPackage/TextTemplates/EFCore5ModelGenerator.ttinclude index f5841ad..57cc04d 100644 --- a/src/DslPackage/TextTemplates/EFCore5ModelGenerator.ttinclude +++ b/src/DslPackage/TextTemplates/EFCore5ModelGenerator.ttinclude @@ -235,51 +235,51 @@ if (!string.IsNullOrEmpty(modelAttribute.InitialValue)) { - if (modelAttribute.InitialValue.Contains(".")) // enum + switch (modelAttribute.Type) { - string enumName = modelAttribute.InitialValue.Split('.').First(); - string enumValue = modelAttribute.InitialValue.Split('.').Last(); - string enumFQN = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName)?.FullName ?? enumName; - segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})"); - } - else - { - if (string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue)) - { - switch (modelAttribute.Type) - { - case "String": - segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")"); + case "String": + segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")"); - break; + break; - case "Char": - segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')"); + case "Char": + segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')"); - break; + break; - case "DateTime": - if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now") - segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); + case "DateTime": + if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now") + segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); - break; + break; - case "DateTimeOffset": - if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now") - segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); + case "DateTimeOffset": + if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now") + segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); - break; + break; - default: - segments.Add($"HasDefaultValue({modelAttribute.InitialValue})"); + default: + string enumName = modelAttribute.InitialValue.Split('.').First(); + ModelEnum enumObject = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName); - break; + // because we may use object properties as a initial value (like DateTime.Now below) we first need to check if it is an enum + if (modelAttribute.InitialValue.Contains(".") && enumObject != null) // enum + { + string enumValue = modelAttribute.InitialValue.Split('.').Last(); + string enumFQN = enumObject.FullName ?? enumName; + segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})"); } - } - else - segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")"); + else + segments.Add($"HasDefaultValue({modelAttribute.InitialValue})"); + + break; } } + else if (!string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue)) + { + segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")"); + } if (!string.IsNullOrEmpty(modelAttribute.DatabaseCollation) && modelAttribute.DatabaseCollation != modelRoot.DatabaseCollationDefault diff --git a/src/DslPackage/TextTemplates/EditingOnly/EFCore5ModelGenerator.cs b/src/DslPackage/TextTemplates/EditingOnly/EFCore5ModelGenerator.cs index 4f644aa..3a6e764 100644 --- a/src/DslPackage/TextTemplates/EditingOnly/EFCore5ModelGenerator.cs +++ b/src/DslPackage/TextTemplates/EditingOnly/EFCore5ModelGenerator.cs @@ -226,51 +226,51 @@ protected override List GatherModelAttributeSegments(ModelAttribute mode if (!string.IsNullOrEmpty(modelAttribute.InitialValue)) { - if (modelAttribute.InitialValue.Contains(".")) // enum + switch (modelAttribute.Type) { - string enumName = modelAttribute.InitialValue.Split('.').First(); - string enumValue = modelAttribute.InitialValue.Split('.').Last(); - string enumFQN = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName)?.FullName ?? enumName; - segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})"); - } - else - { - if (string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue)) - { - switch (modelAttribute.Type) - { - case "String": - segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")"); + case "String": + segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")"); - break; + break; - case "Char": - segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')"); + case "Char": + segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')"); - break; + break; - case "DateTime": - if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now") - segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); + case "DateTime": + if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now") + segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); - break; + break; - case "DateTimeOffset": - if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now") - segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); + case "DateTimeOffset": + if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now") + segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")"); - break; + break; - default: - segments.Add($"HasDefaultValue({modelAttribute.InitialValue})"); + default: + string enumName = modelAttribute.InitialValue.Split('.').First(); + ModelEnum enumObject = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName); - break; + // because we may use object properties as a initial value (like DateTime.Now below) we first need to check if it is an enum + if (modelAttribute.InitialValue.Contains(".") && enumObject != null) // enum + { + string enumValue = modelAttribute.InitialValue.Split('.').Last(); + string enumFQN = enumObject.FullName ?? enumName; + segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})"); } - } - else - segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")"); + else + segments.Add($"HasDefaultValue({modelAttribute.InitialValue})"); + + break; } } + else if (!string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue)) + { + segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")"); + } if (!string.IsNullOrEmpty(modelAttribute.DatabaseCollation) && modelAttribute.DatabaseCollation != modelRoot.DatabaseCollationDefault