Skip to content

Commit 279d758

Browse files
committed
Added EF Core library with support for UtcDateTimeConverter for entities
1 parent 7407cda commit 279d758

3 files changed

Lines changed: 153 additions & 0 deletions

File tree

MADE.NET.sln

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.UI.Styling", "src\MADE
5858
EndProject
5959
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.UI.ViewManagement", "src\MADE.UI.ViewManagement\MADE.UI.ViewManagement.csproj", "{A0893341-0EB6-4EA3-BC80-8F1FBDCBF913}"
6060
EndProject
61+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MADE.Data.EFCore", "src\MADE.Data.EFCore\MADE.Data.EFCore.csproj", "{B5A08402-572D-4ADC-8953-79975915DBC4}"
62+
EndProject
6163
Global
6264
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6365
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -1378,6 +1380,62 @@ Global
13781380
{A0893341-0EB6-4EA3-BC80-8F1FBDCBF913}.Release|x64.Build.0 = Release|Any CPU
13791381
{A0893341-0EB6-4EA3-BC80-8F1FBDCBF913}.Release|x86.ActiveCfg = Release|Any CPU
13801382
{A0893341-0EB6-4EA3-BC80-8F1FBDCBF913}.Release|x86.Build.0 = Release|Any CPU
1383+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1384+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1385+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1386+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1387+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|ARM64.ActiveCfg = Debug|Any CPU
1388+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|ARM64.Build.0 = Debug|Any CPU
1389+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1390+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1391+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1392+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1393+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1394+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1395+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1396+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1397+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1398+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1399+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1400+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|ARM.Build.0 = Debug|Any CPU
1401+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|ARM64.ActiveCfg = Debug|Any CPU
1402+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|ARM64.Build.0 = Debug|Any CPU
1403+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1404+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|iPhone.Build.0 = Debug|Any CPU
1405+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1406+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1407+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|x64.ActiveCfg = Debug|Any CPU
1408+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|x64.Build.0 = Debug|Any CPU
1409+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|x86.ActiveCfg = Debug|Any CPU
1410+
{B5A08402-572D-4ADC-8953-79975915DBC4}.AppStore|x86.Build.0 = Debug|Any CPU
1411+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1412+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1413+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|ARM.ActiveCfg = Debug|Any CPU
1414+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|ARM.Build.0 = Debug|Any CPU
1415+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|ARM64.ActiveCfg = Debug|Any CPU
1416+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|ARM64.Build.0 = Debug|Any CPU
1417+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1418+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|iPhone.Build.0 = Debug|Any CPU
1419+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1420+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1421+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|x64.ActiveCfg = Debug|Any CPU
1422+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|x64.Build.0 = Debug|Any CPU
1423+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|x86.ActiveCfg = Debug|Any CPU
1424+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Debug|x86.Build.0 = Debug|Any CPU
1425+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1426+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|Any CPU.Build.0 = Release|Any CPU
1427+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|ARM.ActiveCfg = Release|Any CPU
1428+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|ARM.Build.0 = Release|Any CPU
1429+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|ARM64.ActiveCfg = Release|Any CPU
1430+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|ARM64.Build.0 = Release|Any CPU
1431+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|iPhone.ActiveCfg = Release|Any CPU
1432+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|iPhone.Build.0 = Release|Any CPU
1433+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1434+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1435+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|x64.ActiveCfg = Release|Any CPU
1436+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|x64.Build.0 = Release|Any CPU
1437+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|x86.ActiveCfg = Release|Any CPU
1438+
{B5A08402-572D-4ADC-8953-79975915DBC4}.Release|x86.Build.0 = Release|Any CPU
13811439
EndGlobalSection
13821440
GlobalSection(SolutionProperties) = preSolution
13831441
HideSolutionNode = FALSE
@@ -1406,6 +1464,7 @@ Global
14061464
{1CDB43BD-71F9-46C6-816C-4EFC9FA2ED1C} = {69149D0F-BB09-411B-88F0-A1E845058D70}
14071465
{68A77F20-D380-421E-B8CC-2EB9F6485062} = {01380FB8-F8A7-4416-AABA-5407574B7723}
14081466
{A0893341-0EB6-4EA3-BC80-8F1FBDCBF913} = {01380FB8-F8A7-4416-AABA-5407574B7723}
1467+
{B5A08402-572D-4ADC-8953-79975915DBC4} = {01380FB8-F8A7-4416-AABA-5407574B7723}
14091468
EndGlobalSection
14101469
GlobalSection(ExtensibilityGlobals) = postSolution
14111470
SolutionGuid = {3921AD86-E6C0-4436-8880-2D9EDFAD6151}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace MADE.Data.EFCore.Converters
2+
{
3+
using System;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
7+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
8+
9+
/// <summary>
10+
/// Defines a converter to help with the storing of dates in a UTC format.
11+
/// </summary>
12+
public static class UtcDateTimeConverter
13+
{
14+
internal static readonly ValueConverter<DateTime, DateTime> UtcConverter =
15+
new ValueConverter<DateTime, DateTime>(
16+
value => value,
17+
value => DateTime.SpecifyKind(value, DateTimeKind.Utc));
18+
19+
private const string IsUtcAnnotation = "IsUtc";
20+
21+
/// <summary>
22+
/// Defines an annotation on a property that it should be in a UTC format.
23+
/// <para>
24+
/// The intended use for this is on properties which are a <see cref="DateTime"/> or DateTime?.
25+
/// </para>
26+
/// </summary>
27+
/// <param name="builder">The property builder.</param>
28+
/// <param name="isUtc">A value indicating whether the property value should be in UTC format.</param>
29+
/// <typeparam name="TProperty">The type of property.</typeparam>
30+
/// <returns>The configured property builder.</returns>
31+
public static PropertyBuilder<TProperty> IsUtc<TProperty>(
32+
this PropertyBuilder<TProperty> builder,
33+
bool isUtc = true)
34+
{
35+
return builder.HasAnnotation(IsUtcAnnotation, isUtc);
36+
}
37+
38+
/// <summary>
39+
/// Determines whether the <paramref name="property"/> has the IsUtc annotation.
40+
/// </summary>
41+
/// <param name="property">The property to check.</param>
42+
/// <returns>A value indicating whether the property has the IsUtc annotation.</returns>
43+
public static bool IsUtc(this IMutableProperty property)
44+
{
45+
return (bool?)property.FindAnnotation(IsUtcAnnotation)?.Value ?? false;
46+
}
47+
48+
/// <summary>
49+
/// Applies a UTC <see cref="DateTime"/> converter to the <paramref name="builder"/>.
50+
/// </summary>
51+
/// <param name="builder">The model builder to apply the converter to.</param>
52+
public static void ApplyUtcDateTimeConverter(this ModelBuilder builder)
53+
{
54+
foreach (IMutableEntityType entityType in builder.Model.GetEntityTypes())
55+
{
56+
foreach (IMutableProperty property in entityType.GetProperties())
57+
{
58+
if (!property.IsUtc())
59+
{
60+
continue;
61+
}
62+
63+
if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?))
64+
{
65+
property.SetValueConverter(UtcConverter);
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="MSBuild.Sdk.Extras">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Product>MADE.NET Data EF Core</Product>
7+
<Description>
8+
This package includes Entity Framework Core helpers such as:
9+
- UtcDateTimeConverter, to help with the storing of dates in a UTC format.
10+
</Description>
11+
<PackageTags>MADE EFCore EntityFramework</PackageTags>
12+
13+
</PropertyGroup>
14+
15+
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
16+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.5" />
17+
</ItemGroup>
18+
19+
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
20+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.14" />
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)