Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date/time value generator + .net conversion #436

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ database:
# schemas to include or empty to include all
schemas:
- dbo

# list of expressions for tables to exclude, source is Schema.TableName
exclude:
- exact: dbo.SchemaVersions
Expand Down Expand Up @@ -90,6 +90,8 @@ data:
directory: '{Project.Directory}\Data\Mapping' # the mapping class output directory
#include XML documentation
document: false
dateTimeKind: Local|Utc # configures the conversion of date/time columns as UTC in .Net. Default: Local
dateTimeDefaultValueGenerator: UtcValueGenerator # defines the default date/time value generator. Default: string.Empty

# query extension class file configuration
query:
Expand Down Expand Up @@ -172,33 +174,33 @@ model:
# script templates
script:
# collection script template with EntityContext as a variable
context:
context:
- templatePath: '.\templates\context.csx' # path to script file
fileName: 'ContextScript.cs' # filename to save script output
directory: '{Project.Directory}\Domain\Context' # directory to save script output
namespace: '{Project.Namespace}.Domain.Context'
namespace: '{Project.Namespace}.Domain.Context'
baseClass: ContextScriptBase
overwrite: true # overwrite existing file
# collection of script template with current Entity as a variable
entity:
- templatePath: '.\templates\entity.csx' # path to script file
fileName: '{Entity.Name}Script.cs' # filename to save script output
directory: '{Project.Directory}\Domain\Entity' # directory to save script output
namespace: '{Project.Namespace}.Domain.Entity'
namespace: '{Project.Namespace}.Domain.Entity'
baseClass: EntityScriptBase
overwrite: true # overwrite existing file
# collection script template with current Model as a variable
model:
- templatePath: '.\templates\model.csx' # path to script file
fileName: '{Model.Name}Script.cs' # filename to save script output
directory: '{Project.Directory}\Domain\Models' # directory to save script output
namespace: '{Project.Namespace}.Domain.Models'
namespace: '{Project.Namespace}.Domain.Models'
baseClass: ModelScriptBase
overwrite: true # overwrite existing file
- templatePath: '.\templates\sample.csx' # path to script file
fileName: '{Model.Name}Sample.cs' # filename to save script output
directory: '{Project.Directory}\Domain\Models' # directory to save script output
namespace: '{Project.Namespace}.Domain.Models'
namespace: '{Project.Namespace}.Domain.Models'
baseClass: ModelSampleBase
overwrite: true # overwrite existing file
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace EntityFrameworkCore.Generator.Options;
using System;
using System.ComponentModel;

namespace EntityFrameworkCore.Generator.Options;

/// <summary>
/// EntityFramework mapping class generation options
Expand All @@ -15,4 +18,16 @@ public MappingClassOptions(VariableDictionary variables, string prefix)
Namespace = "{Project.Namespace}.Data.Mapping";
Directory = @"{Project.Directory}\Data\Mapping";
}
}

/// <summary>
/// Specifies the <see cref="System.DateTimeKind"/> to use for date/time columns.
/// </summary>
[DefaultValue(DateTimeKind.Local)]
public DateTimeKind DateTimeKind { get; set; } = DateTimeKind.Local;

/// <summary>
/// The name of the class that will generate default values for date/time columns that have default value defined in the database.
/// </summary>
[DefaultValue(DateTimeKind.Local)]
public string DateTimeDefaultValueGenerator { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Globalization;
using System.Linq;

using EntityFrameworkCore.Generator.Extensions;
using EntityFrameworkCore.Generator.Metadata.Generation;
using EntityFrameworkCore.Generator.Options;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

Expand Down Expand Up @@ -104,7 +107,7 @@ private void GenerateConstants()

CodeBuilder.AppendLine($"public const string Name = \"{_entity.TableName}\";");
}

CodeBuilder.AppendLine("}");

CodeBuilder.AppendLine();
Expand Down Expand Up @@ -286,6 +289,29 @@ private void GeneratePropertyMapping(Property property)
CodeBuilder.Append($".HasDefaultValueSql({property.Default.ToLiteral()})");
}


if (property.SystemType == typeof(DateTime))
{
if (Options.Data.Mapping.DateTimeKind == DateTimeKind.Utc || property.Default?.IndexOf("utc", StringComparison.OrdinalIgnoreCase) > -1)
{
CodeBuilder.AppendLine();
if (property.IsNullable == false)
{
CodeBuilder.Append($".HasConversion(t => t, t => DateTime.SpecifyKind(t, DateTimeKind.Utc))");
}
else
{
CodeBuilder.Append($".HasConversion(t => t, t => t == null ? null : DateTime.SpecifyKind(t.GetValueOrDefault(), DateTimeKind.Utc))");
}
}

if (!string.IsNullOrEmpty(property.Default) && string.IsNullOrEmpty(Options.Data.Mapping.DateTimeDefaultValueGenerator) == false)
{
CodeBuilder.AppendLine();
CodeBuilder.Append($".HasValueGenerator<{Options.Data.Mapping.DateTimeDefaultValueGenerator}>()");
}
}

switch (property.ValueGenerated)
{
case ValueGenerated.OnAdd:
Expand Down Expand Up @@ -362,4 +388,4 @@ private void GenerateTableMapping()

CodeBuilder.AppendLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="ThisAssembly" Version="1.1.3" />
<PackageReference Include="ThisAssembly" Version="1.2.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down