Skip to content

Commit f205e43

Browse files
Merge pull request #40 from evolvedlight/add-default-mappings
Add default mappings for types. Change visibility to public for mappings to allow compiled ef contexts
2 parents 84d0447 + 0ca8c4e commit f205e43

13 files changed

Lines changed: 145 additions & 18 deletions
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Microsoft.EntityFrameworkCore.SqlServer.Storage;
2+
using NodaTime;
3+
using System;
4+
using Xunit;
5+
6+
namespace SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Tests
7+
{
8+
public class MappingDefaultsTest
9+
{
10+
[Fact]
11+
public void Duration_Defaults()
12+
{
13+
var durationDefault = DurationTypeMapping.Default;
14+
15+
Assert.NotNull(durationDefault);
16+
17+
Assert.Equal("time", durationDefault.StoreType);
18+
Assert.Equal("'02:30:01'", durationDefault.GenerateSqlLiteral(Duration.FromTimeSpan(new TimeSpan(2, 30, 1))));
19+
}
20+
21+
[Fact]
22+
public void Instant_Defaults()
23+
{
24+
var instantDefault = InstantTypeMapping.Default;
25+
26+
Assert.NotNull(instantDefault);
27+
28+
Assert.Equal("datetime2", instantDefault.StoreType);
29+
Assert.Equal("'2024-02-14T00:14:00.0000000Z'", instantDefault.GenerateSqlLiteral(Instant.FromUtc(2024, 02, 14, 0, 14)));
30+
}
31+
32+
[Fact]
33+
public void LocalDateTime_Defaults()
34+
{
35+
var localDateTimeDefault = LocalDateTimeTypeMapping.Default;
36+
37+
Assert.NotNull(localDateTimeDefault);
38+
39+
Assert.Equal("datetime2", localDateTimeDefault.StoreType);
40+
Assert.Equal("'2024-04-01T13:15:00.0000000'", localDateTimeDefault.GenerateSqlLiteral(new LocalDateTime(2024, 04, 01, 13, 15)));
41+
}
42+
43+
[Fact]
44+
public void LocalDate_Defaults()
45+
{
46+
var localDateDefault = LocalDateTypeMapping.Default;
47+
48+
Assert.NotNull(localDateDefault);
49+
50+
Assert.Equal("date", localDateDefault.StoreType);
51+
Assert.Equal("'2024-03-30'", localDateDefault.GenerateSqlLiteral(new LocalDate(2024, 03, 30)));
52+
}
53+
54+
[Fact]
55+
public void LocalTime_Defaults()
56+
{
57+
var localTimeDefault = LocalTimeTypeMapping.Default;
58+
59+
Assert.NotNull(localTimeDefault);
60+
61+
Assert.Equal("time", localTimeDefault.StoreType);
62+
Assert.Equal("'19:40:00'", localTimeDefault.GenerateSqlLiteral(new LocalTime(19, 40)));
63+
}
64+
65+
[Fact]
66+
public void OffsetDateTime_Defaults()
67+
{
68+
var offsetDateTimeDefault = OffsetDateTimeTypeMapping.Default;
69+
70+
Assert.NotNull(offsetDateTimeDefault);
71+
72+
Assert.Equal("datetimeoffset", offsetDateTimeDefault.StoreType);
73+
var localDateTime = new LocalDateTime(2024, 04, 01, 17, 15);
74+
Assert.Equal("'2024-04-01T17:15:00.0000000+02:00'", offsetDateTimeDefault.GenerateSqlLiteral(new OffsetDateTime(localDateTime, Offset.FromHours(2))));
75+
}
76+
}
77+
}

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/DurationTypeMapping.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
99
{
10-
internal class DurationTypeMapping : RelationalTypeMapping
10+
public class DurationTypeMapping : RelationalTypeMapping
1111
{
12+
/// <summary>
13+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
14+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
15+
/// any release. You should only use it directly in your code with extreme caution and knowing that
16+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
17+
/// </summary>
18+
public static DurationTypeMapping Default { get; } = new();
19+
1220
public DurationTypeMapping()
1321
: base(CreateRelationalTypeMappingParameters())
1422
{

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/DurationValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public DurationValueConverter()
1111
{
1212
}
1313

14-
private static TimeSpan toProvider(Duration duration)
14+
public static TimeSpan toProvider(Duration duration)
1515
{
1616
return duration.ToTimeSpan();
1717
}
1818

19-
private static Duration fromProvider(TimeSpan timeSpan)
19+
public static Duration fromProvider(TimeSpan timeSpan)
2020
{
2121
return Duration.FromTimeSpan(timeSpan);
2222
}

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/InstantTypeMapping.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
using Microsoft.EntityFrameworkCore.Storage;
22
using NodaTime;
3+
using SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage;
34
using DateTimeTypeMapping = SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage.DateTimeTypeMapping;
45

56
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
67
{
7-
internal class InstantTypeMapping : DateTimeTypeMapping
8+
public class InstantTypeMapping : DateTimeTypeMapping
89
{
10+
/// <summary>
11+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
12+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
13+
/// any release. You should only use it directly in your code with extreme caution and knowing that
14+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
15+
/// </summary>
16+
public static InstantTypeMapping Default { get; } = new(SqlServerDateTimeTypes.DateTime2);
17+
918
public InstantTypeMapping(string storeType)
1019
: base(storeType, typeof(Instant), new InstantValueConverter())
1120
{

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/InstantValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public InstantValueConverter()
1111
{
1212
}
1313

14-
private static DateTime toProvider(Instant instant)
14+
public static DateTime toProvider(Instant instant)
1515
{
1616
return instant.ToDateTimeUtc();
1717
}
1818

19-
private static Instant fromProvider(DateTime dateTime)
19+
public static Instant fromProvider(DateTime dateTime)
2020
{
2121
return Instant.FromDateTimeUtc(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
2222
}

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/LocalDateTimeTypeMapping.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
using Microsoft.EntityFrameworkCore.Storage;
22
using NodaTime;
3+
using SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage;
34
using DateTimeTypeMapping = SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime.Storage.DateTimeTypeMapping;
45

56
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
67
{
7-
internal class LocalDateTimeTypeMapping : DateTimeTypeMapping
8+
public class LocalDateTimeTypeMapping : DateTimeTypeMapping
89
{
10+
/// <summary>
11+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
12+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
13+
/// any release. You should only use it directly in your code with extreme caution and knowing that
14+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
15+
/// </summary>
16+
public static LocalDateTimeTypeMapping Default { get; } = new(SqlServerDateTimeTypes.DateTime2);
17+
918
public LocalDateTimeTypeMapping(string storeType)
1019
: base(storeType, typeof(LocalDateTime), new LocalDateTimeValueConverter())
1120
{

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/LocalDateTimeValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public LocalDateTimeValueConverter()
1111
{
1212
}
1313

14-
private static DateTime toProvider(LocalDateTime localDateTime)
14+
public static DateTime toProvider(LocalDateTime localDateTime)
1515
{
1616
return localDateTime.ToDateTimeUnspecified();
1717
}
1818

19-
private static LocalDateTime fromProvider(DateTime dateTime)
19+
public static LocalDateTime fromProvider(DateTime dateTime)
2020
{
2121
return LocalDateTime.FromDateTime(dateTime);
2222
}

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/LocalDateTypeMapping.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
1010
{
11-
internal class LocalDateTypeMapping : RelationalTypeMapping
11+
public class LocalDateTypeMapping : RelationalTypeMapping
1212
{
1313
private const string DateFormatConst = "'{0:yyyy-MM-dd}'";
1414

15+
/// <summary>
16+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
17+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
18+
/// any release. You should only use it directly in your code with extreme caution and knowing that
19+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
20+
/// </summary>
21+
public static LocalDateTypeMapping Default { get; } = new();
22+
1523
public LocalDateTypeMapping()
1624
: base(CreateRelationalTypeMappingParameters())
1725
{

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/LocalDateValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public LocalDateValueConverter()
1111
{
1212
}
1313

14-
private static DateTime toProvider(LocalDate localDate)
14+
public static DateTime toProvider(LocalDate localDate)
1515
{
1616
return localDate.ToDateTimeUnspecified();
1717
}
1818

19-
private static LocalDate fromProvider(DateTime dateTime)
19+
public static LocalDate fromProvider(DateTime dateTime)
2020
{
2121
return LocalDate.FromDateTime(dateTime);
2222
}

src/SimplerSoftware.EntityFrameworkCore.SqlServer.NodaTime/Storage/LocalTimeTypeMapping.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage
99
{
10-
internal class LocalTimeTypeMapping : RelationalTypeMapping
10+
public class LocalTimeTypeMapping : RelationalTypeMapping
1111
{
12+
/// <summary>
13+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
14+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
15+
/// any release. You should only use it directly in your code with extreme caution and knowing that
16+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
17+
/// </summary>
18+
public static LocalTimeTypeMapping Default { get; } = new();
19+
1220
public LocalTimeTypeMapping()
1321
: base(CreateRelationalTypeMappingParameters())
1422
{

0 commit comments

Comments
 (0)