-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeFirst examples for .NET 6 and 8 (#785)
- Loading branch information
Showing
39 changed files
with
5,872 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
13 changes: 13 additions & 0 deletions
13
src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.EntityFrameworkCore.DynamicLinq.EFCore6\Microsoft.EntityFrameworkCore.DynamicLinq.EFCore6.csproj" /> | ||
<ProjectReference Include="..\CodeFirst.DataAccess\CodeFirst.DataAccess.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,86 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Linq.Dynamic.Core; | ||
using System.Threading.Tasks; | ||
using CodeFirst.DataAccess.Factories; | ||
using CodeFirst.DataAccess.Repositories; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace CodeFirst.ConsoleApp; | ||
|
||
public static class Program | ||
{ | ||
private static async Task Main() | ||
{ | ||
var sqlFactory = new SqlServerDbContextFactory(); | ||
|
||
await using var context = sqlFactory.CreateDbContext(Array.Empty<string>()); | ||
|
||
var stopwatch = ValueStopwatch.StartNew(); | ||
var query1 = context.Movies.Include(x => x.Copies).Where("Title.Contains(\"e\")").ToDynamicList(); | ||
Console.WriteLine($"Elapsed time: {stopwatch.GetElapsedTime().TotalMilliseconds}ms"); | ||
query1.ToList().ToList().ForEach(Console.WriteLine); | ||
|
||
Console.WriteLine(new string('-', 80)); | ||
|
||
stopwatch = ValueStopwatch.StartNew(); | ||
var query2 = context.Movies.Include(x => x.Copies).Where("Title.Contains(\"e\")").ToDynamicList(); | ||
Console.WriteLine($"Elapsed time: {stopwatch.GetElapsedTime().TotalMilliseconds}ms"); | ||
query2.ToList().ToList().ForEach(Console.WriteLine); | ||
|
||
//var sqlServerRepo = new MoviesRepository(sqlFactory.CreateDbContext(Array.Empty<string>())); | ||
//var movies = await sqlServerRepo.GetAllAsync(); | ||
//movies.ToList().ForEach(Console.WriteLine); | ||
//var getById = await sqlServerRepo.GetByIdAsync(3); | ||
//Console.WriteLine(getById.Title); | ||
|
||
//var postFactory = new PostgresDbContextFactory(); | ||
//var postgresRepo = new MoviesRepository(postFactory.CreateDbContext(Array.Empty<string>())); | ||
//movies = await postgresRepo.GetAllAsync(); | ||
//movies.ToList().ForEach(Console.WriteLine); | ||
//getById = await postgresRepo.GetByIdAsync(3); | ||
//Console.WriteLine(getById.Title); | ||
} | ||
|
||
/// <summary> | ||
/// Copied from https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ValueStopwatch/ValueStopwatch.cs | ||
/// </summary> | ||
internal readonly struct ValueStopwatch | ||
{ | ||
#if !NET7_0_OR_GREATER | ||
private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency; | ||
#endif | ||
|
||
private readonly long _startTimestamp; | ||
|
||
public bool IsActive => _startTimestamp != 0; | ||
|
||
private ValueStopwatch(long startTimestamp) | ||
{ | ||
_startTimestamp = startTimestamp; | ||
} | ||
|
||
public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp()); | ||
|
||
public TimeSpan GetElapsedTime() | ||
{ | ||
// Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0. | ||
// So it being 0 is a clear indication of default(ValueStopwatch) | ||
if (!IsActive) | ||
{ | ||
throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time."); | ||
} | ||
|
||
var end = Stopwatch.GetTimestamp(); | ||
|
||
#if !NET7_0_OR_GREATER | ||
var timestampDelta = end - _startTimestamp; | ||
var ticks = (long)(TimestampToTicks * timestampDelta); | ||
return new TimeSpan(ticks); | ||
#else | ||
return Stopwatch.GetElapsedTime(_startTimestamp, end); | ||
#endif | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\CodeFirst.ConsoleApp\Program.cs" Link="Program.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.EntityFrameworkCore.DynamicLinq.EFCore8\Microsoft.EntityFrameworkCore.DynamicLinq.EFCore8.csproj" /> | ||
<ProjectReference Include="..\CodeFirst.DataAccess\CodeFirst.DataAccess.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
37 changes: 37 additions & 0 deletions
37
src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.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,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' "> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.28" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.28" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.28"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" /> | ||
|
||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.28"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' "> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" /> | ||
|
||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
54 changes: 54 additions & 0 deletions
54
src-examples/CodeFirst.DataAccess/Configurations/ActorsConfiguration.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,54 @@ | ||
using System; | ||
using CodeFirst.DataAccess.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace CodeFirst.DataAccess.Configurations; | ||
|
||
public class ActorsConfiguration : IEntityTypeConfiguration<Actors> | ||
{ | ||
public void Configure(EntityTypeBuilder<Actors> builder) | ||
{ | ||
builder.HasKey(e => e.ActorId) | ||
.HasName("actor_pkey"); | ||
builder.Property(e => e.ActorId) | ||
.HasColumnName("actor_id"); | ||
builder.Property(e => e.Firstname) | ||
.HasColumnName("first_name"); | ||
builder.Property(e => e.Lastname) | ||
.HasColumnName("last_name"); | ||
builder.Property(e => e.Birthday) | ||
.HasColumnName("birthday"); | ||
builder.ToTable("actors"); | ||
|
||
builder.HasData( | ||
new Actors(1, "Arnold", "Schwarzenegger", Convert.ToDateTime("1947-07-30")), | ||
new Actors(2, "Anthony", "Daniels", Convert.ToDateTime("1946-02-21")), | ||
new Actors(3, "Harrison", "Ford", Convert.ToDateTime("1942-07-13")), | ||
new Actors(4, "Carrie", "Fisher", Convert.ToDateTime("1956-10-21")), | ||
new Actors(5, "Alec", "Guiness", Convert.ToDateTime("1914-04-02")), | ||
new Actors(6, "Peter", "Cushing", Convert.ToDateTime("1913-05-26")), | ||
new Actors(7, "David", "Prowse", Convert.ToDateTime("1944-05-19")), | ||
new Actors(8, "Peter", "Mayhew", Convert.ToDateTime("1935-07-01")), | ||
new Actors(9, "Michael", "Biehn", Convert.ToDateTime("1956-07-31")), | ||
new Actors(10, "Linda", "Hamilton", Convert.ToDateTime("1956-09-26")), | ||
new Actors(11, "Bill", "Murray", Convert.ToDateTime("1950-09-21")), | ||
new Actors(12, "Dan", "Aykroyd", Convert.ToDateTime("1952-07-01")), | ||
new Actors(13, "Sigourney", "Weaver", Convert.ToDateTime("1949-10-08")), | ||
new Actors(14, "Robert", "De Niro", Convert.ToDateTime("1943-08-17")), | ||
new Actors(15, "Jodie", "Foster", Convert.ToDateTime("1962-11-19")), | ||
new Actors(16, "Harvey", "Keitel", Convert.ToDateTime("1939-05-13")), | ||
new Actors(17, "Cybill", "Shepherd", Convert.ToDateTime("1950-02-18")), | ||
new Actors(18, "Tom", "Berenger", Convert.ToDateTime("1949-05-31")), | ||
new Actors(19, "Willem", "Dafoe", Convert.ToDateTime("1955-07-22")), | ||
new Actors(20, "Charlie", "Sheen", Convert.ToDateTime("1965-09-03")), | ||
new Actors(21, "Harrison", "Ford", Convert.ToDateTime("1942-07-13")), | ||
new Actors(22, "Emmanuelle", "Seigner", Convert.ToDateTime("1966-06-22")), | ||
new Actors(23, "Jean", "Reno", Convert.ToDateTime("1948-07-30")), | ||
new Actors(24, "Billy", "Crystal", Convert.ToDateTime("1948-03-14")), | ||
new Actors(25, "Lisa", "Kudrow", Convert.ToDateTime("1963-07-30")), | ||
new Actors(26, "Gary", "Oldman", Convert.ToDateTime("1958-03-21")), | ||
new Actors(27, "Natalie", "Portman", Convert.ToDateTime("1981-06-09")), | ||
new Actors(28, "Tom", "Cruise", Convert.ToDateTime("1962-07-03"))); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src-examples/CodeFirst.DataAccess/Configurations/ClientsConfiguration.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,33 @@ | ||
using System; | ||
using CodeFirst.DataAccess.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace CodeFirst.DataAccess.Configurations; | ||
|
||
public class ClientsConfiguration : IEntityTypeConfiguration<Clients> | ||
{ | ||
public void Configure(EntityTypeBuilder<Clients> builder) | ||
{ | ||
builder.HasKey(e => e.ClientId) | ||
.HasName("client_pkey"); | ||
builder.Property(e => e.ClientId) | ||
.HasColumnName("client_id"); | ||
builder.Property(e => e.Firstname) | ||
.HasColumnName("first_name"); | ||
builder.Property(e => e.Lastname) | ||
.HasColumnName("last_name"); | ||
builder.Property(e => e.Birthday) | ||
.HasColumnName("birthday"); | ||
|
||
builder.ToTable("clients"); | ||
|
||
builder.HasData( | ||
new Clients(1, "Hank", "Hill", Convert.ToDateTime("1954-04-19")), | ||
new Clients(2, "Brian", "Griffin", Convert.ToDateTime("2011-09-11")), | ||
new Clients(3, "Gary", "Goodspeed", Convert.ToDateTime("1989-03-12")), | ||
new Clients(4, "Bob", "Belcher", Convert.ToDateTime("1977-01-23")), | ||
new Clients(5, "Lisa", "Simpson", Convert.ToDateTime("2012-05-09")), | ||
new Clients(6, "Rick", "Sanchez", Convert.ToDateTime("1965-03-17"))); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src-examples/CodeFirst.DataAccess/Configurations/CopiesConfiguration.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,46 @@ | ||
using CodeFirst.DataAccess.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace CodeFirst.DataAccess.Configurations; | ||
|
||
public class CopiesConfiguration : IEntityTypeConfiguration<Copies> | ||
{ | ||
public void Configure(EntityTypeBuilder<Copies> builder) | ||
{ | ||
builder.HasKey(e => e.CopyId) | ||
.HasName("copies_pkey"); | ||
|
||
builder.Property(e => e.MovieId) | ||
.IsRequired() | ||
.HasColumnName("movie_id"); | ||
|
||
builder.Property(e => e.CopyId) | ||
.HasColumnName("copy_id"); | ||
|
||
builder.Property(e => e.Available) | ||
.HasColumnName("available"); | ||
|
||
builder.ToTable("copies"); | ||
builder.HasData(new Copies(1, 1, true), | ||
new Copies(2, 1, false), | ||
new Copies(3, 2, true), | ||
new Copies(4, 3, true), | ||
new Copies(5, 3, false), | ||
new Copies(6, 3, true), | ||
new Copies(7, 4, true), | ||
new Copies(8, 5, false), | ||
new Copies(9, 6, true), | ||
new Copies(10, 6, false), | ||
new Copies(11, 6, true), | ||
new Copies(12, 7, true), | ||
new Copies(13, 7, true), | ||
new Copies(14, 8, false), | ||
new Copies(15, 9, true), | ||
new Copies(16, 10, true), | ||
new Copies(17, 10, false), | ||
new Copies(18, 10, true), | ||
new Copies(19, 10, true), | ||
new Copies(20, 10, true)); | ||
} | ||
} |
Oops, something went wrong.