-
Notifications
You must be signed in to change notification settings - Fork 2k
/
PreserveDateTimeKind.cs
104 lines (85 loc) · 3.75 KB
/
PreserveDateTimeKind.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace EFModeling.ValueConversions;
public class PreserveDateTimeKind : Program
{
public void Run()
{
ConsoleWriteLines("Sample showing value conversions for preserving/setting DateTime.Kind...");
using (var context = new SampleDbContext())
{
CleanDatabase(context);
ConsoleWriteLines("Save new entities...");
context.AddRange(
new Post
{
Title = "Post 1",
PostedOn = new DateTime(1973, 9, 3, 0, 0, 0, 0, DateTimeKind.Utc),
LastUpdated = new DateTime(1974, 9, 3, 0, 0, 0, 0, DateTimeKind.Utc),
DeletedOn = new DateTime(2007, 9, 3, 0, 0, 0, 0, DateTimeKind.Utc)
},
new Post
{
Title = "Post 2",
PostedOn = new DateTime(1975, 9, 3, 0, 0, 0, 0, DateTimeKind.Local),
LastUpdated = new DateTime(1976, 9, 3, 0, 0, 0, 0, DateTimeKind.Utc),
DeletedOn = new DateTime(2017, 9, 3, 0, 0, 0, 0, DateTimeKind.Utc)
});
context.SaveChanges();
}
using (var context = new SampleDbContext())
{
ConsoleWriteLines("Read the entities back...");
var blog1 = context.Set<Post>().Single(e => e.Title == "Post 1");
ConsoleWriteLines($"Blog 1: PostedOn.Kind = {blog1.PostedOn.Kind} LastUpdated.Kind = {blog1.LastUpdated.Kind} DeletedOn.Kind = {blog1.DeletedOn.Kind}");
var blog2 = context.Set<Post>().Single(e => e.Title == "Post 2");
ConsoleWriteLines($"Blog 2: PostedOn.Kind = {blog2.PostedOn.Kind} LastUpdated.Kind = {blog2.LastUpdated.Kind} DeletedOn.Kind = {blog2.DeletedOn.Kind}");
}
ConsoleWriteLines("Sample finished.");
}
public class SampleDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region ConfigurePreserveDateTimeKind1
modelBuilder.Entity<Post>()
.Property(e => e.PostedOn)
.HasConversion<long>();
#endregion
#region ConfigurePreserveDateTimeKind2
modelBuilder.Entity<Post>()
.Property(e => e.LastUpdated)
.HasConversion(
v => v,
v => new DateTime(v.Ticks, DateTimeKind.Utc));
#endregion
#region ConfigurePreserveDateTimeKind3
modelBuilder.Entity<Post>()
.Property(e => e.LastUpdated)
.HasConversion(
v => v.ToUniversalTime(),
v => new DateTime(v.Ticks, DateTimeKind.Utc));
#endregion
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted })
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=PreserveDateTimeKind;Trusted_Connection=True;ConnectRetryCount=0")
.EnableSensitiveDataLogging();
}
#region PreserveDateTimeKindModel
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PostedOn { get; set; }
public DateTime LastUpdated { get; set; }
public DateTime DeletedOn { get; set; }
}
#endregion
}