Skip to content

Commit

Permalink
Add CodeFirst examples for .NET 6 and 8 (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Apr 6, 2024
1 parent 1a345d8 commit f8eddd6
Show file tree
Hide file tree
Showing 39 changed files with 5,872 additions and 0 deletions.
62 changes: 62 additions & 0 deletions System.Linq.Dynamic.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RadzenDataGrid.BlazorApp",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_net6.0_EF6_Sqlite", "src-console\ConsoleAppEF6_Sqlite\ConsoleApp_net6.0_EF6_Sqlite.csproj", "{CA03FD55-9DAB-4827-9A35-A96D3804B311}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src-examples", "src-examples", "{BCA2A024-9032-4E56-A6C4-17A15D921728}"
ProjectSection(SolutionItems) = preProject
src-examples\README.md = src-examples\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.DataAccess", "src-examples\CodeFirst.DataAccess\CodeFirst.DataAccess.csproj", "{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.ConsoleApp", "src-examples\CodeFirst.ConsoleApp\CodeFirst.ConsoleApp.csproj", "{9E0D0994-7D84-40FF-8383-189F142FEF11}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.ConsoleApp8", "src-examples\CodeFirst.ConsoleApp8\CodeFirst.ConsoleApp8.csproj", "{68C7FF71-54F6-4D68-B419-65D1B10206D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -858,6 +869,54 @@ Global
{51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x64.Build.0 = Release|Any CPU
{51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x86.ActiveCfg = Release|Any CPU
{51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x86.Build.0 = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|ARM.ActiveCfg = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|ARM.Build.0 = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x64.ActiveCfg = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x64.Build.0 = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x86.ActiveCfg = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x86.Build.0 = Debug|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|Any CPU.Build.0 = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|ARM.ActiveCfg = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|ARM.Build.0 = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x64.ActiveCfg = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x64.Build.0 = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x86.ActiveCfg = Release|Any CPU
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x86.Build.0 = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|ARM.ActiveCfg = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|ARM.Build.0 = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x64.ActiveCfg = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x64.Build.0 = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x86.ActiveCfg = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x86.Build.0 = Debug|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|Any CPU.Build.0 = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|ARM.ActiveCfg = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|ARM.Build.0 = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x64.ActiveCfg = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x64.Build.0 = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x86.ActiveCfg = Release|Any CPU
{9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x86.Build.0 = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|ARM.ActiveCfg = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|ARM.Build.0 = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x64.ActiveCfg = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x64.Build.0 = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x86.ActiveCfg = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x86.Build.0 = Debug|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|Any CPU.Build.0 = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|ARM.ActiveCfg = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|ARM.Build.0 = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x64.ActiveCfg = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x64.Build.0 = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x86.ActiveCfg = Release|Any CPU
{68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x86.Build.0 = Release|Any CPU
{CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -923,6 +982,9 @@ Global
{9000129D-322D-4FE6-9C47-75464577C374} = {DBD7D9B6-FCC7-4650-91AF-E6457573A68F}
{ABB1BF71-8927-49BB-99F3-70BCB2CD161E} = {8463ED7E-69FB-49AE-85CF-0791AFD98E38}
{51074A4C-15C2-4E72-81F2-2FC53903553B} = {122BC4FA-7563-4E35-9D17-077F16F1629F}
{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4} = {BCA2A024-9032-4E56-A6C4-17A15D921728}
{9E0D0994-7D84-40FF-8383-189F142FEF11} = {BCA2A024-9032-4E56-A6C4-17A15D921728}
{68C7FF71-54F6-4D68-B419-65D1B10206D4} = {BCA2A024-9032-4E56-A6C4-17A15D921728}
{CA03FD55-9DAB-4827-9A35-A96D3804B311} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
13 changes: 13 additions & 0 deletions src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.csproj
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>
86 changes: 86 additions & 0 deletions src-examples/CodeFirst.ConsoleApp/Program.cs
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 src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.csproj
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 src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.csproj
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>
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")));
}
}
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")));
}
}
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));
}
}
Loading

0 comments on commit f8eddd6

Please sign in to comment.