Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tomlane/SampleProject
#1 Add a sample project
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...tyFrameworkCore.Conversions.Sample/NodaTime.EntityFrameworkCore.Conversions.Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NodaTime.EntityFrameworkCore.Conversions\NodaTime.EntityFrameworkCore.Conversions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
71 changes: 71 additions & 0 deletions
71
src/NodaTime.EntityFrameworkCore.Conversions.Sample/NodaTimeContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace NodaTime.EntityFrameworkCore.Conversions.Sample | ||
{ | ||
partial class Program | ||
{ | ||
internal class NodaTimeContext : DbContext | ||
{ | ||
public NodaTimeContext() | ||
{ } | ||
|
||
public NodaTimeContext(DbContextOptions<NodaTimeContext> contextOptions) | ||
: base(contextOptions) | ||
{ } | ||
|
||
public DbSet<TimeData> TimeData { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseInMemoryDatabase("NodaTimeSample"); | ||
|
||
base.OnConfiguring(optionsBuilder); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.Duration) | ||
.HasConversion(new DurationValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.Instant) | ||
.HasConversion(new InstantValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.LocalDateTime) | ||
.HasConversion(new LocalDateTimeValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.LocalDate) | ||
.HasConversion(new LocalDateValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.LocalTime) | ||
.HasConversion(new LocalTimeValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.OffsetDateTime) | ||
.HasConversion(new OffsetDateTimeValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.Offset) | ||
.HasConversion(new OffsetValueConverter()); | ||
|
||
modelBuilder | ||
.Entity<TimeData>() | ||
.Property(p => p.ZonedDateTime) | ||
.HasConversion(new ZonedDateTimeValueConverter()); | ||
|
||
base.OnModelCreating(modelBuilder); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/NodaTime.EntityFrameworkCore.Conversions.Sample/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace NodaTime.EntityFrameworkCore.Conversions.Sample | ||
{ | ||
partial class Program | ||
{ | ||
static void Main() | ||
{ | ||
var timeData = new TimeData | ||
{ | ||
Duration = Duration.FromMinutes(1), | ||
Offset = Offset.FromHours(1), | ||
ZonedDateTime = new ZonedDateTime(Instant.FromDateTimeUtc(DateTime.UtcNow), DateTimeZone.Utc), | ||
LocalDateTime = LocalDateTime.FromDateTime(DateTime.Today), | ||
OffsetDateTime = OffsetDateTime.FromDateTimeOffset(DateTimeOffset.UtcNow), | ||
LocalDate = LocalDate.FromDateTime(DateTime.Today), | ||
Instant = Instant.FromDateTimeUtc(DateTime.UtcNow), | ||
LocalTime = new LocalTime(18, 6), | ||
Id = Guid.NewGuid() | ||
}; | ||
|
||
var context = new NodaTimeContext(); | ||
|
||
context.TimeData.Add(timeData); | ||
context.SaveChanges(); | ||
|
||
var newData = context.TimeData.FirstOrDefault(x => x.Id == timeData.Id); | ||
|
||
Console.WriteLine($"Duration: {newData.Duration}. Offset: {newData.Offset}. ZonedDateTime: {newData.ZonedDateTime}. " + | ||
$"LocalDateTime: {newData.LocalDateTime}. OffsetDateTime: {newData.LocalDateTime}. LocalDate: {newData.LocalDate}. " + | ||
$"LocalTime: {newData.LocalTime}. Id: {newData.Id}."); | ||
Console.ReadLine(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/NodaTime.EntityFrameworkCore.Conversions.Sample/TimeData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace NodaTime.EntityFrameworkCore.Conversions.Sample | ||
{ | ||
internal class TimeData | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public Duration Duration { get; set; } | ||
public Instant Instant { get; set; } | ||
public LocalDateTime LocalDateTime { get; set; } | ||
public LocalDate LocalDate { get; set; } | ||
public LocalTime LocalTime { get; set; } | ||
public OffsetDateTime OffsetDateTime { get; set; } | ||
public Offset Offset { get; set; } | ||
public ZonedDateTime ZonedDateTime { get; set; } | ||
} | ||
} |