-
Notifications
You must be signed in to change notification settings - Fork 13
/
DateSample.cs
57 lines (54 loc) · 2.39 KB
/
DateSample.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
// Copyright 2021 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.Cloud.EntityFrameworkCore.Spanner.Samples.SampleModel;
using Google.Cloud.EntityFrameworkCore.Spanner.Storage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
/// <summary>
/// The Clr type <see cref="DateTime"/> is often used for both dates and timestamps. Cloud Spanner has two distinct
/// data types for DATE and TIMESTAMP. To distinguish between the two in Entity Framework Core, it is recommended to
/// use <see cref="SpannerDate"/> to map DATE columns and <see cref="DateTime"/> to map TIMESTAMP columns.
///
/// Run from the command line with `dotnet run DateSample`
/// </summary>
public static class DateSample
{
public static async Task Run(string connectionString)
{
using var context = new SpannerSampleDbContext(connectionString);
var singer = new Singer
{
SingerId = Guid.NewGuid(),
FirstName = "Yvette",
LastName = "Wendelson",
// SpannerDate is specifically designed to map a DATE column in a Cloud Spanner
// database to a property of an entity. DateTime properties will by default be
// mapped to TIMESTAMP columns.
BirthDate = new SpannerDate(1980, 10, 17),
};
await context.Singers.AddAsync(singer);
await context.SaveChangesAsync();
// Commonly used properties and methods of SpannerDate are mapped to the equivalent Cloud Spanner functions.
var singersBornIn1980 = await context.Singers
.Where(s => s.BirthDate.GetValueOrDefault().Year == 1980)
.OrderBy(s => singer.LastName)
.ToListAsync();
foreach (var s in singersBornIn1980)
{
Console.WriteLine($"Born in 1980: {s.FullName} ({s.BirthDate})");
}
}
}