diff --git a/1_CS/LINQ/DataLib/DataLib.csproj b/1_CS/LINQ/DataLib/DataLib.csproj index fc76d702..0dec0a0b 100644 --- a/1_CS/LINQ/DataLib/DataLib.csproj +++ b/1_CS/LINQ/DataLib/DataLib.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable enable diff --git a/1_CS/LINQ/DataLib/Formula1.cs b/1_CS/LINQ/DataLib/Formula1.cs index 6e0408a1..d5c152e5 100644 --- a/1_CS/LINQ/DataLib/Formula1.cs +++ b/1_CS/LINQ/DataLib/Formula1.cs @@ -5,47 +5,47 @@ public static class Formula1 private static List? s_racers; public static IList GetChampions() => s_racers ??= InitializeRacers(); - private static List InitializeRacers() => new() - { - new("Nino", "Farina", "Italy", 33, 5, new[] { 1950 }, new[] { "Alfa Romeo" }), - new("Alberto", "Ascari", "Italy", 32, 13, new[] { 1952, 1953 }, new[] { "Ferrari" }), - new("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }), - new("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" }), - new("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" }), - new("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" }), - new("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" }), - new("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" }), - new("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" }), - new("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" }), - new("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" }), - new("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" }), - new("Emerson", "Fittipaldi", "Brazil", 144, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" }), - new("James", "Hunt", "UK", 92, 10, new int[] { 1976 }, new string[] { "McLaren" }), - new("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" }), - new("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" }), - new("Alan", "Jones", "Australia", 116, 12, new int[] { 1980 }, new string[] { "Williams" }), - new("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" }), - new("Niki", "Lauda", "Austria", 171, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" }), - new("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" }), - new("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" }), - new("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" }), - new("Alain", "Prost", "France", 199, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" }), - new("Damon", "Hill", "UK", 115, 22, new int[] { 1996 }, new string[] { "Williams" }), - new("Jacques", "Villeneuve", "Canada", 163, 11, new int[] { 1997 }, new string[] { "Williams" }), - new("Mika", "Hakkinen", "Finland", 161, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" }), - new("Michael", "Schumacher", "Germany", 306, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }), - new("Fernando", "Alonso", "Spain", 370, 32, new int[] { 2005, 2006 }, new string[] { "Renault" }), - new("Kimi", "Räikkönen", "Finland", 349, 21, new int[] { 2007 }, new string[] { "Ferrari" }), - new("Jenson", "Button", "UK", 306, 16, new int[] { 2009 }, new string[] { "Brawn GP" }), - new("Sebastian", "Vettel", "Germany", 299, 53, new int[] { 2010, 2011, 2012, 2013 }, new string[] { "Red Bull Racing" }), - new("Nico", "Rosberg", "Germany", 206, 24, new int[] { 2016 }, new string[] { "Mercedes" }), - new("Lewis", "Hamilton", "UK", 322, 103, new int[] { 2008, 2014, 2015, 2017, 2018, 2019, 2020 }, new string[] { "McLaren", "Mercedes" }), - new("Max", "Verstappen", "Netherlands", 175, 45, new int[] { 2021, 2022 }, new string[] { "Red Bull Racing" }) - }; + private static List InitializeRacers() => + [ + new("Nino", "Farina", "Italy", 33, 5, [1950], ["Alfa Romeo"]), + new("Alberto", "Ascari", "Italy", 32, 13, [1952, 1953], ["Ferrari"]), + new("Juan Manuel", "Fangio", "Argentina", 51, 24, [1951, 1954, 1955, 1956, 1957], ["Alfa Romeo", "Maserati", "Mercedes", "Ferrari"]), + new("Mike", "Hawthorn", "UK", 45, 3, [1958], ["Ferrari"]), + new("Phil", "Hill", "USA", 48, 3, [1961], ["Ferrari"]), + new("John", "Surtees", "UK", 111, 6, [1964], ["Ferrari"]), + new("Jim", "Clark", "UK", 72, 25, [1963, 1965], ["Lotus"]), + new("Jack", "Brabham", "Australia", 125, 14, [1959, 1960, 1966], ["Cooper", "Brabham"]), + new("Denny", "Hulme", "New Zealand", 112, 8, [1967], ["Brabham"]), + new("Graham", "Hill", "UK", 176, 14, [1962, 1968], ["BRM", "Lotus"]), + new("Jochen", "Rindt", "Austria", 60, 6, [1970], ["Lotus"]), + new("Jackie", "Stewart", "UK", 99, 27, [1969, 1971, 1973], ["Matra", "Tyrrell"]), + new("Emerson", "Fittipaldi", "Brazil", 144, 14, [1972, 1974], ["Lotus", "McLaren"]), + new("James", "Hunt", "UK", 92, 10, [1976], ["McLaren"]), + new("Mario", "Andretti", "USA", 128, 12, [1978], ["Lotus"]), + new("Jody", "Scheckter", "South Africa", 112, 10, [1979], ["Ferrari"]), + new("Alan", "Jones", "Australia", 116, 12, [1980], ["Williams"]), + new("Keke", "Rosberg", "Finland", 114, 5, [1982], ["Williams"]), + new("Niki", "Lauda", "Austria", 171, 25, [1975, 1977, 1984], ["Ferrari", "McLaren"]), + new("Nelson", "Piquet", "Brazil", 204, 23, [1981, 1983, 1987], ["Brabham", "Williams"]), + new("Ayrton", "Senna", "Brazil", 161, 41, [1988, 1990, 1991], ["McLaren"]), + new("Nigel", "Mansell", "UK", 187, 31, [1992], ["Williams"]), + new("Alain", "Prost", "France", 199, 51, [1985, 1986, 1989, 1993], ["McLaren", "Williams"]), + new("Damon", "Hill", "UK", 115, 22, [1996], ["Williams"]), + new("Jacques", "Villeneuve", "Canada", 163, 11, [1997], ["Williams"]), + new("Mika", "Hakkinen", "Finland", 161, 20, [1998, 1999], ["McLaren"]), + new("Michael", "Schumacher", "Germany", 306, 91, [1994, 1995, 2000, 2001, 2002, 2003, 2004], ["Benetton", "Ferrari"]), + new("Fernando", "Alonso", "Spain", 404, 32, [2005, 2006], ["Renault"]), + new("Kimi", "Räikkönen", "Finland", 349, 21, [2007], ["Ferrari"]), + new("Jenson", "Button", "UK", 306, 16, [2009], ["Brawn GP"]), + new("Sebastian", "Vettel", "Germany", 299, 53, [2010, 2011, 2012, 2013], ["Red Bull Racing"]), + new("Nico", "Rosberg", "Germany", 206, 24, [2016], ["Mercedes"]), + new("Lewis", "Hamilton", "UK", 335, 105, [2008, 2014, 2015, 2017, 2018, 2019, 2020], ["McLaren", "Mercedes"]), + new("Max", "Verstappen", "Netherlands", 209, 63, [2021, 2022, 2023, 2024], ["Red Bull Racing"]) + ]; private static List? s_teams; - public static IList GetConstructorChampions() => s_teams ??= new() - { + public static IList GetConstructorChampions() => s_teams ??= + [ new("Vanwall", 1958), new("Cooper", 1959, 1960), new("BRM", 1962), @@ -55,17 +55,17 @@ public static class Formula1 new("Lotus", 1963, 1965, 1968, 1970, 1972, 1973, 1978), new("Benetton", 1995), new("Williams", 1980, 1981, 1986, 1987, 1992, 1993, 1994, 1996, 1997), - new("McLaren", 1974, 1984, 1985, 1988, 1989, 1990, 1991, 1998), + new("McLaren", 1974, 1984, 1985, 1988, 1989, 1990, 1991, 1998, 2024), new("Renault", 2005, 2006), new("Ferrari", 1961, 1964, 1975, 1976, 1977, 1979, 1982, 1983, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008), new("Brawn GP", 2009), - new("Red Bull Racing", 2010, 2011, 2012, 2013, 2022), + new("Red Bull Racing", 2010, 2011, 2012, 2013, 2022, 2023), new("Mercedes", 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021) - }; + ]; private static List? s_championships; - public static IEnumerable GetChampionships() => s_championships ??= new() - { + public static IEnumerable GetChampionships() => s_championships ??= + [ new(1950, "Nino Farina", "Juan Manuel Fangio", "Luigi Fagioli"), new(1951, "Juan Manuel Fangio", "Alberto Ascari", "Froilan Gonzalez"), new(1952, "Alberto Ascari", "Nino Farina", "Piero Taruffi"), @@ -139,11 +139,13 @@ public static class Formula1 new(2020, "Lewis Hamilton", "Valtteri Bottas", "Max Verstappen"), new(2021, "Max Verstappen", "Lewis Hamilton", "Valtteri Bottas"), new(2022, "Max Verstappen", "Charles Leclerc","Sergio Perez"), - }; + new Championship(2023, "Max Verstappen", "Sergio Perez", "Lewis Hamilton"), + new Championship(2024, "Max Verstappen", "Lando Norris", "Charles Leclerc") + ]; private static List? s_moreRacers; - public static IList GetMoreRacers() => s_moreRacers ??= new() - { + public static IList GetMoreRacers() => s_moreRacers ??= + [ new Racer("Luigi", "Fagioli", "Italy", Starts: 7, Wins: 1), new Racer("Jose Froilan", "Gonzalez", "Argentina", 26, 2), new Racer("Piero", "Taruffi", "Italy", Starts: 18, Wins: 1), @@ -175,9 +177,10 @@ public static class Formula1 new Racer("Juan Pablo", "Montoya", "Columbia", Starts: 94, Wins: 7), new Racer("Felipe", "Massa", "Brazil", Starts: 269, Wins: 11), new Racer("Mark", "Webber", "Australia", Starts: 215, Wins: 9), - new Racer("Daniel", "Ricciardo", "Australia", Starts: 234, Wins: 8), - new Racer("Valtteri", "Bottas", "Finland", Starts: 212, Wins: 10), - new Racer("Charles", "Leclerc", "Monaco", Starts: 115, Wins: 5), - new Racer("Sergio", "Perez", "Mexico", Starts: 248, Wins: 6), - }; -} + new Racer("Daniel", "Ricciardo", "Australia", Starts: 257, Wins: 8), + new Racer("Valtteri", "Bottas", "Finland", Starts: 246, Wins: 10), + new Racer("Charles", "Leclerc", "Monaco", Starts: 149, Wins: 8), + new Racer("Sergio", "Perez", "Mexico", Starts: 282, Wins: 6), + new Racer("Lando", "Norris", "UK", Starts: 128, Wins: 4) + ]; +} \ No newline at end of file diff --git a/1_CS/LINQ/DataLib/Racer.cs b/1_CS/LINQ/DataLib/Racer.cs index 4a4fb73e..12a4b75a 100644 --- a/1_CS/LINQ/DataLib/Racer.cs +++ b/1_CS/LINQ/DataLib/Racer.cs @@ -3,7 +3,7 @@ public record Racer(string FirstName, string LastName, string Country, int Starts, int Wins, IEnumerable Years, IEnumerable Cars) : IComparable, IFormattable { public Racer(string FirstName, string LastName, string Country, int Starts, int Wins) - : this(FirstName, LastName, Country, Starts, Wins, new int[] { }, new string[] { }) + : this(FirstName, LastName, Country, Starts, Wins, [], []) { } public override string ToString() => $"{FirstName} {LastName}"; diff --git a/1_CS/LINQ/DataLib/Team.cs b/1_CS/LINQ/DataLib/Team.cs index d64e85f5..efe3a2a6 100644 --- a/1_CS/LINQ/DataLib/Team.cs +++ b/1_CS/LINQ/DataLib/Team.cs @@ -5,7 +5,7 @@ public record Team public Team(string name, params int[] years) { Name = name; - Years = years is not null ? new List(years) : new List(); + Years = years is not null ? [.. years] : []; } public string Name { get; } public IEnumerable Years { get; } diff --git a/1_CS/LINQ/EnumerableSample/EnumerableSample.csproj b/1_CS/LINQ/EnumerableSample/EnumerableSample.csproj index a23ab309..aeb2f54f 100644 --- a/1_CS/LINQ/EnumerableSample/EnumerableSample.csproj +++ b/1_CS/LINQ/EnumerableSample/EnumerableSample.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable diff --git a/1_CS/LINQ/ExpressionTreeSample/ExpressionTreeSample.csproj b/1_CS/LINQ/ExpressionTreeSample/ExpressionTreeSample.csproj index 027de805..db9c3ad4 100644 --- a/1_CS/LINQ/ExpressionTreeSample/ExpressionTreeSample.csproj +++ b/1_CS/LINQ/ExpressionTreeSample/ExpressionTreeSample.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable diff --git a/1_CS/LINQ/LinqIntro/LinqIntro.csproj b/1_CS/LINQ/LinqIntro/LinqIntro.csproj index 027de805..db9c3ad4 100644 --- a/1_CS/LINQ/LinqIntro/LinqIntro.csproj +++ b/1_CS/LINQ/LinqIntro/LinqIntro.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable diff --git a/1_CS/LINQ/ParallelLinqSample/ParallelLinqSample.csproj b/1_CS/LINQ/ParallelLinqSample/ParallelLinqSample.csproj index 027de805..db9c3ad4 100644 --- a/1_CS/LINQ/ParallelLinqSample/ParallelLinqSample.csproj +++ b/1_CS/LINQ/ParallelLinqSample/ParallelLinqSample.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable diff --git a/2_Libs/EFCore/BooksLib/Book.cs b/2_Libs/EFCore/BooksLib/Book.cs index 287d2466..1710a235 100644 --- a/2_Libs/EFCore/BooksLib/Book.cs +++ b/2_Libs/EFCore/BooksLib/Book.cs @@ -2,19 +2,13 @@ namespace BooksLib; -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; [StringLength(20)] public string? Isbn { get; set; } } diff --git a/2_Libs/EFCore/BooksLib/BooksContext.cs b/2_Libs/EFCore/BooksLib/BooksContext.cs index ab3e3289..416f00a3 100644 --- a/2_Libs/EFCore/BooksLib/BooksContext.cs +++ b/2_Libs/EFCore/BooksLib/BooksContext.cs @@ -2,11 +2,8 @@ namespace BooksLib; -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); diff --git a/2_Libs/EFCore/BooksLib/BooksLib.csproj b/2_Libs/EFCore/BooksLib/BooksLib.csproj index e74cc547..017acd43 100644 --- a/2_Libs/EFCore/BooksLib/BooksLib.csproj +++ b/2_Libs/EFCore/BooksLib/BooksLib.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 enable enable - - + + diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/Book.cs b/2_Libs/EFCore/ConflictHandling-FirstWins/Book.cs index 125003db..ea3e984c 100644 --- a/2_Libs/EFCore/ConflictHandling-FirstWins/Book.cs +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/Book.cs @@ -1,16 +1,11 @@ using System.ComponentModel.DataAnnotations; +namespace ConfictHandling; -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; } \ No newline at end of file diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/BooksContext.cs b/2_Libs/EFCore/ConflictHandling-FirstWins/BooksContext.cs index 181cc7cf..e7417a7d 100644 --- a/2_Libs/EFCore/ConflictHandling-FirstWins/BooksContext.cs +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/BooksContext.cs @@ -1,10 +1,7 @@ -using Microsoft.EntityFrameworkCore; +namespace ConfictHandling; -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { var sampleBooks = GetSampleBooks(); @@ -16,7 +13,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .IsRowVersion(); } - private IEnumerable GetSampleBooks() + private static IEnumerable GetSampleBooks() => Enumerable.Range(1, 100) .Select(id => new Book($"title {id}", "sample", id)).ToArray(); diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/ConflictHandling-FirstWins.csproj b/2_Libs/EFCore/ConflictHandling-FirstWins/ConflictHandling-FirstWins.csproj index 1556ee32..51855e26 100644 --- a/2_Libs/EFCore/ConflictHandling-FirstWins/ConflictHandling-FirstWins.csproj +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/ConflictHandling-FirstWins.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable @@ -12,13 +12,13 @@ --> - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/GlobalUsings.cs b/2_Libs/EFCore/ConflictHandling-FirstWins/GlobalUsings.cs new file mode 100644 index 00000000..7a891cab --- /dev/null +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/GlobalUsings.cs @@ -0,0 +1,7 @@ +global using ConfictHandling; + +global using Microsoft.EntityFrameworkCore; +global using Microsoft.EntityFrameworkCore.ChangeTracking; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/Program.cs b/2_Libs/EFCore/ConflictHandling-FirstWins/Program.cs index 34a78a20..e10a7115 100644 --- a/2_Libs/EFCore/ConflictHandling-FirstWins/Program.cs +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/Program.cs @@ -1,16 +1,10 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - + using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { - var connectionString = context.Configuration.GetConnectionString("BooksConnection"); - services.AddDbContext(options => - { - options.UseSqlServer(connectionString); - }); + string connectionString = context.Configuration.GetConnectionString("BooksConnection") ?? throw new InvalidOperationException("Could not read BooksConnection"); + services.AddDbContext(options => + options.UseSqlServer(connectionString)); services.AddScoped(); }) .Build(); diff --git a/2_Libs/EFCore/ConflictHandling-FirstWins/Runner.cs b/2_Libs/EFCore/ConflictHandling-FirstWins/Runner.cs index b91dc992..95c5cc6b 100644 --- a/2_Libs/EFCore/ConflictHandling-FirstWins/Runner.cs +++ b/2_Libs/EFCore/ConflictHandling-FirstWins/Runner.cs @@ -1,12 +1,10 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; +namespace ConfictHandling; -public class Runner +public class Runner(BooksContext booksContext) { - private readonly BooksContext _booksContext; + private readonly BooksContext _booksContext = booksContext; private string? _user; private Book? _selectedBook; - public Runner(BooksContext booksContext) => _booksContext = booksContext; public async Task CreateTheDatabaseAsync() { @@ -41,7 +39,8 @@ public async Task PrepareUpdateAsync(string user, int id = 0) public async Task UpdateAsync() { - if (_selectedBook is null || _user is null) throw new InvalidOperationException("_selectedBook not set. Invoke PrepareUpdateAsync before UpdateAsync"); + if (_selectedBook is null || _user is null) + throw new InvalidOperationException("_selectedBook not set. Invoke PrepareUpdateAsync before UpdateAsync"); try { @@ -68,8 +67,8 @@ public async Task UpdateAsync() } } - private void ShowChanges(int id, EntityEntry entity) - { + private static void ShowChanges(int id, EntityEntry entity) + { static void ShowChange(PropertyEntry propertyEntry, int id) => Console.WriteLine($"id: {id}, current: {propertyEntry.CurrentValue}, " + $"original: {propertyEntry.OriginalValue}, " + @@ -82,7 +81,8 @@ static void ShowChange(PropertyEntry propertyEntry, int id) => public async Task GetUpdatedTitleAsyc(int id) { var book = await _booksContext.Books.FindAsync(id); - if (book is null) return string.Empty; + if (book is null) + return string.Empty; return $"{book.Title} with id {book.BookId}"; } } @@ -91,7 +91,8 @@ internal static class Converter { public static string TimestampToString(this PropertyEntry entry) { - if (entry.CurrentValue is null) return string.Empty; + if (entry.CurrentValue is null) + return string.Empty; byte[] data = (byte[])entry.CurrentValue; return BitConverter.ToString(data); } diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/Book.cs b/2_Libs/EFCore/ConflictHandling-LastWins/Book.cs index 5a424e2f..7e255c8f 100644 --- a/2_Libs/EFCore/ConflictHandling-LastWins/Book.cs +++ b/2_Libs/EFCore/ConflictHandling-LastWins/Book.cs @@ -1,16 +1,10 @@ -using System.ComponentModel.DataAnnotations; +namespace ConflictHandling; -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; } diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/BooksContext.cs b/2_Libs/EFCore/ConflictHandling-LastWins/BooksContext.cs index 407692be..b13fa0c2 100644 --- a/2_Libs/EFCore/ConflictHandling-LastWins/BooksContext.cs +++ b/2_Libs/EFCore/ConflictHandling-LastWins/BooksContext.cs @@ -1,10 +1,7 @@ -using Microsoft.EntityFrameworkCore; +namespace ConflictHandling; -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { var sampleBooks = GetSampleBooks(); @@ -12,7 +9,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) base.OnModelCreating(modelBuilder); } - private IEnumerable GetSampleBooks() + private Book[] GetSampleBooks() => Enumerable.Range(1, 100) .Select(id => new Book($"title {id}", "sample", id)).ToArray(); diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/ConflictHandling-LastWins.csproj b/2_Libs/EFCore/ConflictHandling-LastWins/ConflictHandling-LastWins.csproj index 1556ee32..51855e26 100644 --- a/2_Libs/EFCore/ConflictHandling-LastWins/ConflictHandling-LastWins.csproj +++ b/2_Libs/EFCore/ConflictHandling-LastWins/ConflictHandling-LastWins.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable @@ -12,13 +12,13 @@ --> - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/GlobalUsings.cs b/2_Libs/EFCore/ConflictHandling-LastWins/GlobalUsings.cs new file mode 100644 index 00000000..b43e5434 --- /dev/null +++ b/2_Libs/EFCore/ConflictHandling-LastWins/GlobalUsings.cs @@ -0,0 +1,8 @@ +global using System.ComponentModel.DataAnnotations; + +global using Microsoft.EntityFrameworkCore; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; + +global using ConflictHandling; \ No newline at end of file diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/Program.cs b/2_Libs/EFCore/ConflictHandling-LastWins/Program.cs index 35c1865a..2b6733f9 100644 --- a/2_Libs/EFCore/ConflictHandling-LastWins/Program.cs +++ b/2_Libs/EFCore/ConflictHandling-LastWins/Program.cs @@ -1,12 +1,8 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - + using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { - var connectionString = context.Configuration.GetConnectionString("BooksConnection"); + string connectionString = context.Configuration.GetConnectionString("BooksConnection") ?? throw new InvalidOperationException("Could not read BooksConnection"); services.AddDbContext(options => { options.UseSqlServer(connectionString); diff --git a/2_Libs/EFCore/ConflictHandling-LastWins/Runner.cs b/2_Libs/EFCore/ConflictHandling-LastWins/Runner.cs index 69edde5d..e3c94e40 100644 --- a/2_Libs/EFCore/ConflictHandling-LastWins/Runner.cs +++ b/2_Libs/EFCore/ConflictHandling-LastWins/Runner.cs @@ -1,14 +1,10 @@ -using Microsoft.EntityFrameworkCore; +namespace ConflictHandling; -public class Runner +public class Runner(BooksContext booksContext) { - private readonly BooksContext _booksContext; + private readonly BooksContext _booksContext = booksContext; private string? _user; private Book? _selectedBook; - public Runner(BooksContext booksContext) - { - _booksContext = booksContext; - } public async Task CreateTheDatabaseAsync() { @@ -43,7 +39,8 @@ public async Task PrepareUpdateAsync(string user, int id = 0) public async Task UpdateAsync() { - if (_selectedBook is null) throw new InvalidOperationException("_selectedBook not set. Invoke PrepareUpdateAsync before UpdateAsync"); + if (_selectedBook is null) + throw new InvalidOperationException("_selectedBook not set. Invoke PrepareUpdateAsync before UpdateAsync"); _selectedBook.Title = $"Book updated from {_user}"; int records = await _booksContext.SaveChangesAsync(); if (records == 1) @@ -55,7 +52,8 @@ public async Task UpdateAsync() public async Task GetUpdatedTitleAsyc(int id) { var book = await _booksContext.Books.FindAsync(id); - if (book is null) return String.Empty; + if (book is null) + return string.Empty; return $"{book.Title} with id {book.BookId}"; } } diff --git a/2_Libs/EFCore/Cosmos/Cosmos.csproj b/2_Libs/EFCore/Cosmos/Cosmos.csproj index 3fc2cce4..62c117b4 100644 --- a/2_Libs/EFCore/Cosmos/Cosmos.csproj +++ b/2_Libs/EFCore/Cosmos/Cosmos.csproj @@ -2,21 +2,21 @@ Exe - net7.0 + net8.0 enable enable 6a71cc89-6908-4ebf-81da-6c43fdc7f321 - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/Cosmos/MenuItem.cs b/2_Libs/EFCore/Cosmos/MenuItem.cs index b25516e0..ebab78ee 100644 --- a/2_Libs/EFCore/Cosmos/MenuItem.cs +++ b/2_Libs/EFCore/Cosmos/MenuItem.cs @@ -1,9 +1,7 @@ -public class MenuItem +public class MenuItem(string text, Guid menuItemId = default) { - public MenuItem(string text, Guid menuItemId = default) => (Text, MenuItemId) = (text, menuItemId); - - public Guid MenuItemId { get; set; } - public string Text { get; set; } + public Guid MenuItemId { get; set; } = menuItemId; + public string Text { get; set; } = text; public decimal? Price { get; set; } public override string ToString() => Text; diff --git a/2_Libs/EFCore/Intro/Book.cs b/2_Libs/EFCore/Intro/Book.cs index 510dd33f..a82b563d 100644 --- a/2_Libs/EFCore/Intro/Book.cs +++ b/2_Libs/EFCore/Intro/Book.cs @@ -9,19 +9,13 @@ public record Book( #else -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; } #endif \ No newline at end of file diff --git a/2_Libs/EFCore/Intro/BooksContext.cs b/2_Libs/EFCore/Intro/BooksContext.cs index 2cba0dcc..9c9b3e89 100644 --- a/2_Libs/EFCore/Intro/BooksContext.cs +++ b/2_Libs/EFCore/Intro/BooksContext.cs @@ -1,8 +1,5 @@ -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); diff --git a/2_Libs/EFCore/Intro/Intro.csproj b/2_Libs/EFCore/Intro/Intro.csproj index 1556ee32..51855e26 100644 --- a/2_Libs/EFCore/Intro/Intro.csproj +++ b/2_Libs/EFCore/Intro/Intro.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable @@ -12,13 +12,13 @@ --> - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/Intro/Runner.cs b/2_Libs/EFCore/Intro/Runner.cs index fb7a0095..fa4a8b36 100644 --- a/2_Libs/EFCore/Intro/Runner.cs +++ b/2_Libs/EFCore/Intro/Runner.cs @@ -1,10 +1,6 @@ -public class Runner +public class Runner(BooksContext booksContext) { - private readonly BooksContext _booksContext; - public Runner(BooksContext booksContext) - { - _booksContext = booksContext; - } + private readonly BooksContext _booksContext = booksContext; public async Task CreateTheDatabaseAsync() { diff --git a/2_Libs/EFCore/LoadingRelatedData/Book.cs b/2_Libs/EFCore/LoadingRelatedData/Book.cs index 08192d52..896d0463 100644 --- a/2_Libs/EFCore/LoadingRelatedData/Book.cs +++ b/2_Libs/EFCore/LoadingRelatedData/Book.cs @@ -1,16 +1,10 @@ -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; // set accessor required for lazy loading public virtual ICollection Chapters { get; protected set; } = new HashSet(); diff --git a/2_Libs/EFCore/LoadingRelatedData/BooksContext.cs b/2_Libs/EFCore/LoadingRelatedData/BooksContext.cs index d912ef2d..2d200d83 100644 --- a/2_Libs/EFCore/LoadingRelatedData/BooksContext.cs +++ b/2_Libs/EFCore/LoadingRelatedData/BooksContext.cs @@ -1,10 +1,7 @@ using Microsoft.EntityFrameworkCore; -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity
().HasData(GetSampleAddresses()); @@ -14,29 +11,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) base.OnModelCreating(modelBuilder); } - private readonly Random _random = new Random(); - - private IEnumerable GetSampleBooks() + private Book[] GetSampleBooks() => Enumerable.Range(1, 100) - .Select(id => new Book($"title {id}", "sample", id) { Publisher = _publishers[_random.Next(0, 2)], AuthorId = _random.Next(1, 3) }) + .Select(id => new Book($"title {id}", "sample", id) { Publisher = _publishers[Random.Shared.Next(0, 2)], AuthorId = Random.Shared.Next(1, 3) }) .ToArray(); - private IEnumerable GetSampleChapters() + private Chapter[] GetSampleChapters() => Enumerable.Range(1, 100) .Select(id => new Chapter($"chapter {id % 10}", id) { BookId = (id / 10 + 1) }) .ToArray(); - private readonly string[] _publishers = { "pub1", "pub2", "pub3" }; - private readonly string[] _countries = { "country1", "country2", "country3" }; - private readonly string[] _cities = { "city1", "city2", "city3" }; - private readonly string[] _firstNames = { "first1", "first2", "first3" }; - private readonly string[] _lastNames = {"last1", "last2", "last3"}; - private IEnumerable
GetSampleAddresses() + private readonly string[] _publishers = ["pub1", "pub2", "pub3"]; + private readonly string[] _countries = ["country1", "country2", "country3"]; + private readonly string[] _cities = ["city1", "city2", "city3"]; + private readonly string[] _firstNames = ["first1", "first2", "first3"]; + private readonly string[] _lastNames = ["last1", "last2", "last3"]; + private Address[] GetSampleAddresses() => Enumerable.Range(1, 3) .Select(i => new Address { AddressId = i, Country = _countries[i - 1], City = _cities[i - 1]}) .ToArray(); - private IEnumerable GetSamplePeople() + private Person[] GetSamplePeople() => Enumerable.Range(1, 3) .Select(i => new Person(_firstNames[i - 1], _lastNames[i - 1], i) { AddressId = i }) .ToArray(); diff --git a/2_Libs/EFCore/LoadingRelatedData/Chapter.cs b/2_Libs/EFCore/LoadingRelatedData/Chapter.cs index f3239695..9432327d 100644 --- a/2_Libs/EFCore/LoadingRelatedData/Chapter.cs +++ b/2_Libs/EFCore/LoadingRelatedData/Chapter.cs @@ -1,12 +1,7 @@ -public class Chapter +public class Chapter(string title, int chapterId = 0) { - public Chapter(string title, int chapterId = 0) - { - Title = title; - ChapterId = chapterId; - } - public int ChapterId { get; set; } - public string Title { get; set; } + public int ChapterId { get; set; } = chapterId; + public string Title { get; set; } = title; public int BookId { get; set; } public virtual Book? Book { get; set; } } diff --git a/2_Libs/EFCore/LoadingRelatedData/LoadingRelatedData.csproj b/2_Libs/EFCore/LoadingRelatedData/LoadingRelatedData.csproj index 7b9779e1..90885104 100644 --- a/2_Libs/EFCore/LoadingRelatedData/LoadingRelatedData.csproj +++ b/2_Libs/EFCore/LoadingRelatedData/LoadingRelatedData.csproj @@ -2,20 +2,20 @@ Exe - net7.0 + net8.0 enable enable - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/2_Libs/EFCore/LoadingRelatedData/Person.cs b/2_Libs/EFCore/LoadingRelatedData/Person.cs index cb005a56..ec70f25a 100644 --- a/2_Libs/EFCore/LoadingRelatedData/Person.cs +++ b/2_Libs/EFCore/LoadingRelatedData/Person.cs @@ -1,16 +1,9 @@ -public class Person +public class Person(string firstName, string lastName, int personId = 0) { - public Person(string firstName, string lastName, int personId = 0) - { - FirstName = firstName; - LastName = lastName; - PersonId = personId; - } + public int PersonId { get; private set; } = personId; - public int PersonId { get; private set; } - - public string FirstName { get; set; } - public string LastName { get; set; } + public string FirstName { get; set; } = firstName; + public string LastName { get; set; } = lastName; public int AddressId { get; set; } [ForeignKey(nameof(AddressId))] diff --git a/2_Libs/EFCore/LoadingRelatedData/Runner.cs b/2_Libs/EFCore/LoadingRelatedData/Runner.cs index 5101e936..889c7f39 100644 --- a/2_Libs/EFCore/LoadingRelatedData/Runner.cs +++ b/2_Libs/EFCore/LoadingRelatedData/Runner.cs @@ -1,8 +1,6 @@ -public class Runner +public class Runner(BooksContext booksContext) { - private readonly BooksContext _booksContext; - - public Runner(BooksContext booksContext) => _booksContext = booksContext; + private readonly BooksContext _booksContext = booksContext; public async Task CreateTheDatabaseAsync() { @@ -69,7 +67,8 @@ public async Task ExplicitLoadingAsync() await bookEntry.Reference(b => b.Author).LoadAsync(); Console.WriteLine($"{book.Author?.FirstName} {book.Author?.LastName}"); - if (book.Author is null) continue; // no author, fix CS8634 warning + if (book.Author is null) + continue; // no author, fix CS8634 warning await _booksContext.Entry(book.Author).Reference(a => a.Address).LoadAsync(); Console.WriteLine($"{book.Author!.Address!.Country}"); diff --git a/2_Libs/EFCore/MigrationApp/MigrationApp.csproj b/2_Libs/EFCore/MigrationApp/MigrationApp.csproj index fafff537..d6404801 100644 --- a/2_Libs/EFCore/MigrationApp/MigrationApp.csproj +++ b/2_Libs/EFCore/MigrationApp/MigrationApp.csproj @@ -2,14 +2,14 @@ Exe - net6.0 + net8.0 enable enable - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/2_Libs/EFCore/Models/MenuCard.cs b/2_Libs/EFCore/Models/MenuCard.cs index 24049ebc..8bdeca70 100644 --- a/2_Libs/EFCore/Models/MenuCard.cs +++ b/2_Libs/EFCore/Models/MenuCard.cs @@ -1,10 +1,7 @@ -public class MenuCard +public class MenuCard(string title, int menuCardId = default) { - public MenuCard(string title, int menuCardId = default) - => (Title, MenuCardId) = (title, menuCardId); - - public int MenuCardId { get; set; } - public string Title { get; set; } + public int MenuCardId { get; set; } = menuCardId; + public string Title { get; set; } = title; public ICollection MenuItems { get; } = new List(); public override string ToString() => Title; } diff --git a/2_Libs/EFCore/Models/MenuItem.cs b/2_Libs/EFCore/Models/MenuItem.cs index 0abfadcc..faeed523 100644 --- a/2_Libs/EFCore/Models/MenuItem.cs +++ b/2_Libs/EFCore/Models/MenuItem.cs @@ -1,9 +1,7 @@ -public class MenuItem +public class MenuItem(string text, int menuItemId = default) { - public MenuItem(string text, int menuItemId = default) => (Text, MenuItemId) = (text, menuItemId); - - public int MenuItemId { get; set; } - public string Text { get; set; } + public int MenuItemId { get; set; } = menuItemId; + public string Text { get; set; } = text; public decimal? Price { get; set; } private MenuCard? _menuCard; public MenuCard MenuCard diff --git a/2_Libs/EFCore/Models/MenusContext.cs b/2_Libs/EFCore/Models/MenusContext.cs index dba90aab..4b3b8f34 100644 --- a/2_Libs/EFCore/Models/MenusContext.cs +++ b/2_Libs/EFCore/Models/MenusContext.cs @@ -1,8 +1,5 @@ -class MenusContext : DbContext +class MenusContext(DbContextOptions options) : DbContext(options) { - public MenusContext(DbContextOptions options) - : base(options) {} - public DbSet MenuCards => Set(); public DbSet MenuItems => Set(); public DbSet Restaurants => Set(); @@ -47,7 +44,7 @@ private IEnumerable GetInitialMenus(dynamic card, Guid restaurantId) Price = 6.5M, IsDeleted = false, LastUpdated = now, - MenuCardId = card.MenuCardId, + card.MenuCardId, RestaurantId = restaurantId }).ToArray(); } @@ -57,9 +54,9 @@ public override Task SaveChangesAsync(CancellationToken cancellationToken = ChangeTracker.DetectChanges(); foreach (var item in ChangeTracker.Entries() - .Where(e => e.State == EntityState.Added - || e.State == EntityState.Modified - || e.State == EntityState.Deleted)) + .Where(e => e.State is EntityState.Added + or EntityState.Modified + or EntityState.Deleted)) { item.CurrentValues[LastUpdated] = DateTime.Now; diff --git a/2_Libs/EFCore/Models/Models.csproj b/2_Libs/EFCore/Models/Models.csproj index a14a098e..4ff49212 100644 --- a/2_Libs/EFCore/Models/Models.csproj +++ b/2_Libs/EFCore/Models/Models.csproj @@ -2,19 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/Models/Restaurant.cs b/2_Libs/EFCore/Models/Restaurant.cs index b29c1c6e..83fbb7c0 100644 --- a/2_Libs/EFCore/Models/Restaurant.cs +++ b/2_Libs/EFCore/Models/Restaurant.cs @@ -2,9 +2,12 @@ { public Restaurant(string name, int id = default) => (_name, _id) = (name, id); - private int _id = default; - private string _name; +#pragma warning disable IDE0032 // Use auto property - field names are used with EF Core mapping + private readonly int _id = default; + private readonly string _name; + public string Name => _name; +#pragma warning restore IDE0032 // Use auto property public override string ToString() => $"{Name}, {_id}"; } diff --git a/2_Libs/EFCore/Models/Runner.cs b/2_Libs/EFCore/Models/Runner.cs index fbc2e2c5..8c9fed99 100644 --- a/2_Libs/EFCore/Models/Runner.cs +++ b/2_Libs/EFCore/Models/Runner.cs @@ -1,10 +1,6 @@ -class Runner +class Runner(MenusContext menusContext) { - private readonly MenusContext _menusContext; - public Runner(MenusContext menusContext) - { - _menusContext = menusContext; - } + private readonly MenusContext _menusContext = menusContext; public Task CreateDatabaseAsync() { @@ -14,7 +10,8 @@ public Task CreateDatabaseAsync() public async Task DeleteMenuItemsAsync(int id) { MenuItem? menuItem = await _menusContext.MenuItems.FindAsync(id); - if (menuItem is null) return; + if (menuItem is null) + return; _menusContext.Remove(menuItem); int records = await _menusContext.SaveChangesAsync(); diff --git a/2_Libs/EFCore/Queries/CompiledQueryExtensions.cs b/2_Libs/EFCore/Queries/CompiledQueryExtensions.cs index 567b9dda..1b771347 100644 --- a/2_Libs/EFCore/Queries/CompiledQueryExtensions.cs +++ b/2_Libs/EFCore/Queries/CompiledQueryExtensions.cs @@ -10,10 +10,7 @@ private static Func> CompileMenusByT public static IEnumerable MenuItemsByText(this MenusContext menusContext, string text) { - if (s_menuItemsByText is null) - { - s_menuItemsByText = CompileMenusByTextQuery(); - } + s_menuItemsByText ??= CompileMenusByTextQuery(); return s_menuItemsByText(menusContext, text); } @@ -24,10 +21,7 @@ private static Func> CompileMen public static IAsyncEnumerable MenuItemsByTextAsync(this MenusContext menusContext, string text) { - if (s_menuItemsByTextAsync is null) - { - s_menuItemsByTextAsync = CompileMenuItemsByTextAsyncQuery(); - } + s_menuItemsByTextAsync ??= CompileMenuItemsByTextAsyncQuery(); return s_menuItemsByTextAsync(menusContext, text); } } diff --git a/2_Libs/EFCore/Queries/MenuCard.cs b/2_Libs/EFCore/Queries/MenuCard.cs index 24049ebc..8bdeca70 100644 --- a/2_Libs/EFCore/Queries/MenuCard.cs +++ b/2_Libs/EFCore/Queries/MenuCard.cs @@ -1,10 +1,7 @@ -public class MenuCard +public class MenuCard(string title, int menuCardId = default) { - public MenuCard(string title, int menuCardId = default) - => (Title, MenuCardId) = (title, menuCardId); - - public int MenuCardId { get; set; } - public string Title { get; set; } + public int MenuCardId { get; set; } = menuCardId; + public string Title { get; set; } = title; public ICollection MenuItems { get; } = new List(); public override string ToString() => Title; } diff --git a/2_Libs/EFCore/Queries/MenuItem.cs b/2_Libs/EFCore/Queries/MenuItem.cs index e0f51390..eaac0393 100644 --- a/2_Libs/EFCore/Queries/MenuItem.cs +++ b/2_Libs/EFCore/Queries/MenuItem.cs @@ -1,9 +1,7 @@ -public class MenuItem +public class MenuItem(string text, int menuItemId = default) { - public MenuItem(string text, int menuItemId = default) => (Text, MenuItemId) = (text, menuItemId); - - public int MenuItemId { get; set; } - public string Text { get; set; } + public int MenuItemId { get; set; } = menuItemId; + public string Text { get; set; } = text; public decimal? Price { get; set; } private MenuCard? _menuCard; public MenuCard MenuCard diff --git a/2_Libs/EFCore/Queries/MenusContext.cs b/2_Libs/EFCore/Queries/MenusContext.cs index 9ced133f..429a493f 100644 --- a/2_Libs/EFCore/Queries/MenusContext.cs +++ b/2_Libs/EFCore/Queries/MenusContext.cs @@ -2,11 +2,8 @@ using static ColumnNames; -class MenusContext : DbContext +class MenusContext(DbContextOptions options) : DbContext(options) { - public MenusContext(DbContextOptions options) - : base(options) {} - public DbSet MenuCards => Set(); public DbSet MenuItems => Set(); public DbSet Restaurants => Set(); @@ -55,7 +52,7 @@ private IEnumerable GetInitialMenus(dynamic card, Guid restaurantId, in Price = 6.5M, IsDeleted = false, LastUpdated = now, - MenuCardId = card.MenuCardId, + card.MenuCardId, RestaurantId = restaurantId }).ToArray(); } @@ -65,9 +62,9 @@ public override Task SaveChangesAsync(CancellationToken cancellationToken = ChangeTracker.DetectChanges(); foreach (var item in ChangeTracker.Entries() - .Where(e => e.State == EntityState.Added - || e.State == EntityState.Modified - || e.State == EntityState.Deleted)) + .Where(e => e.State is EntityState.Added + or EntityState.Modified + or EntityState.Deleted)) { item.CurrentValues[LastUpdated] = DateTime.Now; diff --git a/2_Libs/EFCore/Queries/Program.cs b/2_Libs/EFCore/Queries/Program.cs index 39200ef3..4aaf5f5c 100644 --- a/2_Libs/EFCore/Queries/Program.cs +++ b/2_Libs/EFCore/Queries/Program.cs @@ -30,5 +30,3 @@ await runner.UseEFunctions("24"); await runner.DeleteDatabaseAsync(); - - diff --git a/2_Libs/EFCore/Queries/Queries.csproj b/2_Libs/EFCore/Queries/Queries.csproj index a6c38b35..1132db9e 100644 --- a/2_Libs/EFCore/Queries/Queries.csproj +++ b/2_Libs/EFCore/Queries/Queries.csproj @@ -2,15 +2,15 @@ Exe - net7.0 + net8.0 enable enable - - - + + + diff --git a/2_Libs/EFCore/Queries/Restaurant.cs b/2_Libs/EFCore/Queries/Restaurant.cs index b29c1c6e..1614c5d6 100644 --- a/2_Libs/EFCore/Queries/Restaurant.cs +++ b/2_Libs/EFCore/Queries/Restaurant.cs @@ -2,9 +2,12 @@ { public Restaurant(string name, int id = default) => (_name, _id) = (name, id); - private int _id = default; - private string _name; +#pragma warning disable IDE0032 // Use auto property - _id and _name are used with EF Core mapping + private readonly int _id; + private readonly string _name; + public string Name => _name; +#pragma warning restore IDE0032 // Use auto property public override string ToString() => $"{Name}, {_id}"; } diff --git a/2_Libs/EFCore/Queries/Runner.cs b/2_Libs/EFCore/Queries/Runner.cs index 484d3548..a46becd7 100644 --- a/2_Libs/EFCore/Queries/Runner.cs +++ b/2_Libs/EFCore/Queries/Runner.cs @@ -1,9 +1,8 @@ using Microsoft.EntityFrameworkCore; -class Runner +class Runner(MenusContext menusContext) { - private readonly MenusContext _menusContext; - public Runner(MenusContext menusContext) => _menusContext = menusContext; + private readonly MenusContext _menusContext = menusContext; public Task CreateDatabaseAsync() { diff --git a/2_Libs/EFCore/Relationships/Bank/BankContext.cs b/2_Libs/EFCore/Relationships/Bank/BankContext.cs index 4aa36d02..3258b961 100644 --- a/2_Libs/EFCore/Relationships/Bank/BankContext.cs +++ b/2_Libs/EFCore/Relationships/Bank/BankContext.cs @@ -1,8 +1,5 @@ -public class BankContext : DbContext +public class BankContext(DbContextOptions options) : DbContext(options) { - public BankContext(DbContextOptions options) - : base(options) {} - public DbSet Payments => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/2_Libs/EFCore/Relationships/Bank/BankRunner.cs b/2_Libs/EFCore/Relationships/Bank/BankRunner.cs index 86452ee1..54178b6f 100644 --- a/2_Libs/EFCore/Relationships/Bank/BankRunner.cs +++ b/2_Libs/EFCore/Relationships/Bank/BankRunner.cs @@ -1,8 +1,6 @@ -public class BankRunner +public class BankRunner(BankContext bankContext) { - private readonly BankContext _bankContext; - - public BankRunner(BankContext bankContext) => _bankContext = bankContext; + private readonly BankContext _bankContext = bankContext; public async Task CreateTheDatabaseAsync() { diff --git a/2_Libs/EFCore/Relationships/Bank/Payments.cs b/2_Libs/EFCore/Relationships/Bank/Payments.cs index 88b700b8..ea178040 100644 --- a/2_Libs/EFCore/Relationships/Bank/Payments.cs +++ b/2_Libs/EFCore/Relationships/Bank/Payments.cs @@ -1,19 +1,13 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -public abstract class Payment +public abstract class Payment(string name, decimal amount, int paymentId = 0) { - public Payment(string name, decimal amount, int paymentId = 0) - { - Name = name; - Amount = amount; - PaymentId = paymentId; - } - public int PaymentId { get; init; } + public int PaymentId { get; init; } = paymentId; [StringLength(20)] - public string Name { get; set; } + public string Name { get; set; } = name; [Column(TypeName = "Money")] - public decimal Amount { get; set; } + public decimal Amount { get; set; } = amount; } public class CashPayment : Payment @@ -22,9 +16,7 @@ public CashPayment(string name, decimal amount, int paymentId = 0) : base(name, amount, paymentId) { } } -public class CreditcardPayment : Payment +public class CreditcardPayment(string name, decimal amount, int paymentId = 0) : Payment(name, amount, paymentId) { - public CreditcardPayment(string name, decimal amount, int paymentId = 0) - : base(name, amount, paymentId) { } public string? CreditcardNumber { get; set; } } diff --git a/2_Libs/EFCore/Relationships/Books/Book.cs b/2_Libs/EFCore/Relationships/Books/Book.cs index 4e0602a6..7deba160 100644 --- a/2_Libs/EFCore/Relationships/Books/Book.cs +++ b/2_Libs/EFCore/Relationships/Books/Book.cs @@ -1,19 +1,12 @@ using System.ComponentModel.DataAnnotations; -public class Book +public class Book(string title, string? publisher = default, int bookId = default) { - public Book(string title, string? publisher = default, int bookId = default) - { - Title = title; - Publisher = publisher; - BookId = bookId; - } - [StringLength(50)] - public string Title { get; set; } + public string Title { get; set; } = title; [StringLength(30)] - public string? Publisher { get; set; } - public int BookId { get; set; } + public string? Publisher { get; set; } = publisher; + public int BookId { get; set; } = bookId; public DateTime? ReleaseDate { get; set; } public ICollection Authors { get; } = new HashSet(); diff --git a/2_Libs/EFCore/Relationships/Books/BooksContext.cs b/2_Libs/EFCore/Relationships/Books/BooksContext.cs index f5f9dd19..e6010890 100644 --- a/2_Libs/EFCore/Relationships/Books/BooksContext.cs +++ b/2_Libs/EFCore/Relationships/Books/BooksContext.cs @@ -1,8 +1,5 @@ -public class BooksContext : DbContext +public class BooksContext(DbContextOptions options) : DbContext(options) { - public BooksContext(DbContextOptions options) - : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("bk"); diff --git a/2_Libs/EFCore/Relationships/Books/BooksRunner.cs b/2_Libs/EFCore/Relationships/Books/BooksRunner.cs index 8540da42..e06b6e18 100644 --- a/2_Libs/EFCore/Relationships/Books/BooksRunner.cs +++ b/2_Libs/EFCore/Relationships/Books/BooksRunner.cs @@ -1,8 +1,6 @@ -public class BooksRunner +public class BooksRunner(BooksContext booksContext) { - private readonly BooksContext _booksContext; - - public BooksRunner(BooksContext booksContext) => _booksContext = booksContext; + private readonly BooksContext _booksContext = booksContext; public async Task CreateTheDatabaseAsync() { @@ -22,20 +20,20 @@ public async Task CreateTheDatabaseAsync() private void AddAuthorsToBooks(Book[] books, Person[] authors) { int[][] bookAuthors = - { - new int[] { 0, 1, 2, 3, 4, 5, 6 }, - new int[] { 0, 1, 2, 3, 4, 6, 7, 8, 9 }, - new int[] { 0, 1, 6, 9, 10 }, - new int[] { 1, 3, 6, 9, 10, 11 }, - new int[] { 1, 3, 6, 9, 10 }, - new int[] { 1, 3, 6, 9, 10 }, - new int[] { 1, 3, 6, 9, 10 }, - new int[] { 1, 3, 9 }, - new int[] { 1, 3, 9 }, - new int[] { 1 }, - new int[] { 1 }, - new int[] { 1 }, - }; + [ + [0, 1, 2, 3, 4, 5, 6], + [0, 1, 2, 3, 4, 6, 7, 8, 9], + [0, 1, 6, 9, 10], + [1, 3, 6, 9, 10, 11], + [1, 3, 6, 9, 10], + [1, 3, 6, 9, 10], + [1, 3, 6, 9, 10], + [1, 3, 9], + [1, 3, 9], + [1], + [1], + [1], + ]; for (int i = 0; i < books.Length; i++) { @@ -65,7 +63,7 @@ private void AddAuthorsToBooks(Book[] books, Person[] authors) private Book[] GetBooks() { string[] titles = - { + [ "Professional C#", // CN, Simon Robinson, Ollie Cornes, Jay Glynn, Burton Harvey, Craig McQueen, Karli Watson "Professional C# 2nd Edition", // CN, Simon Robinson, K S Allen, Ollie Cornes, Jay Glynn, Zach Greenvoss, Burton Harvey, Morgan Skinner, Karli Watson "Professional C# 3rd Edition", // CN, Simon Robinson, Karli Watson, Morgan Skinner, Bill Evjen @@ -78,10 +76,10 @@ private Book[] GetBooks() "Professional C# 6 and .NET Core 1.0", // CN "Professional C# 7 and .NET Core 2.0", // CN "Professional C# and .NET 2021 Edition" // CN - }; + ]; DateTime?[] dates = - { + [ new DateTime(2001, 6, 1), // .NET 1 Beta 2 new DateTime(2003, 2, 28), // .NET 1 new DateTime(2004, 6, 4), // .NET 1.1 @@ -94,14 +92,14 @@ private Book[] GetBooks() new DateTime(2016, 4, 29), // C# 6, .NET Core 1 new DateTime(2018, 5, 29), // C# 7, .NET Core 2.1 new DateTime(2021, 8, 31), // C# 9, .NET 5 - }; + ]; - List books = new(); + List books = []; for (int i = 0; i < titles.Length; i++) { books.Add(new Book(titles[i], "Wrox Press") { ReleaseDate = dates[i] }); } - return books.ToArray(); + return [.. books]; } public async Task GetBooksForAuthorAsync() diff --git a/2_Libs/EFCore/Relationships/Books/Person.cs b/2_Libs/EFCore/Relationships/Books/Person.cs index 2ba6311e..527c2d85 100644 --- a/2_Libs/EFCore/Relationships/Books/Person.cs +++ b/2_Libs/EFCore/Relationships/Books/Person.cs @@ -1,27 +1,19 @@ -public class Person +public class Person(string firstName, string lastName, int personId = 0) { - public Person(string firstName, string lastName, int personId = 0) - { - FirstName = firstName; - LastName = lastName; - PersonId = personId; - BusinessAddress = new Address(); - } - public Person(string firstName, string lastName, Address businessAddress, int personId = 0) : this(firstName, lastName, personId) { BusinessAddress = businessAddress; } - public int PersonId { get; private set; } + public int PersonId { get; private set; } = personId; - public string FirstName { get; set; } - public string LastName { get; set; } + public string FirstName { get; set; } = firstName; + public string LastName { get; set; } = lastName; public ICollection WrittenBooks = new HashSet(); - public Address BusinessAddress { get; set; } + public Address BusinessAddress { get; set; } = new Address(); public Address? PrivateAddress { get; set; } } diff --git a/2_Libs/EFCore/Relationships/Menus/MenuItem.cs b/2_Libs/EFCore/Relationships/Menus/MenuItem.cs index dc495e4c..7ea912e4 100644 --- a/2_Libs/EFCore/Relationships/Menus/MenuItem.cs +++ b/2_Libs/EFCore/Relationships/Menus/MenuItem.cs @@ -1,12 +1,7 @@ -public class MenuItem +public class MenuItem(string title, int menuItemId = 0) { - public MenuItem(string title, int menuItemId = 0) - { - Title = title; - MenuItemId = menuItemId; - } - public int MenuItemId { get; init; } - public string Title { get; set; } + public int MenuItemId { get; init; } = menuItemId; + public string Title { get; set; } = title; public string? Subtitle { get; set; } public decimal Price { get; set; } public MenuDetails? Details { get; set; } diff --git a/2_Libs/EFCore/Relationships/Menus/MenusContext.cs b/2_Libs/EFCore/Relationships/Menus/MenusContext.cs index 91650bf8..07d875b2 100644 --- a/2_Libs/EFCore/Relationships/Menus/MenusContext.cs +++ b/2_Libs/EFCore/Relationships/Menus/MenusContext.cs @@ -1,8 +1,5 @@ -public class MenusContext : DbContext +public class MenusContext(DbContextOptions options) : DbContext(options) { - public MenusContext(DbContextOptions options) - : base(options) { } - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("ms"); diff --git a/2_Libs/EFCore/Relationships/Menus/MenusRunner.cs b/2_Libs/EFCore/Relationships/Menus/MenusRunner.cs index 9c5f3135..8e3910d0 100644 --- a/2_Libs/EFCore/Relationships/Menus/MenusRunner.cs +++ b/2_Libs/EFCore/Relationships/Menus/MenusRunner.cs @@ -1,16 +1,12 @@ -public class MenusRunner +public class MenusRunner(MenusContext menusContext) { - private readonly MenusContext _menusContext; - - public MenusRunner(MenusContext menusContext) => _menusContext = menusContext; - public async Task CreateTheDatabaseAsync() { - await _menusContext.Database.MigrateAsync(); + await menusContext.Database.MigrateAsync(); } public async Task DeleteTheDatabaseAsync() { - await _menusContext.Database.EnsureDeletedAsync(); + await menusContext.Database.EnsureDeletedAsync(); } } diff --git a/2_Libs/EFCore/Relationships/Relationships.csproj b/2_Libs/EFCore/Relationships/Relationships.csproj index 1556ee32..066fedb4 100644 --- a/2_Libs/EFCore/Relationships/Relationships.csproj +++ b/2_Libs/EFCore/Relationships/Relationships.csproj @@ -2,23 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/ScaffoldSample/ScaffoldSample.csproj b/2_Libs/EFCore/ScaffoldSample/ScaffoldSample.csproj index 382a7b63..f52b1ae0 100644 --- a/2_Libs/EFCore/ScaffoldSample/ScaffoldSample.csproj +++ b/2_Libs/EFCore/ScaffoldSample/ScaffoldSample.csproj @@ -8,11 +8,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/2_Libs/EFCore/Tracking/MenuCard.cs b/2_Libs/EFCore/Tracking/MenuCard.cs index c5651cea..2032f2b9 100644 --- a/2_Libs/EFCore/Tracking/MenuCard.cs +++ b/2_Libs/EFCore/Tracking/MenuCard.cs @@ -1,10 +1,7 @@ -public class MenuCard +public class MenuCard(string title, Guid menuCardId = default) { - public MenuCard(string title, Guid menuCardId = default) - => (Title, MenuCardId) = (title, menuCardId); - - public Guid MenuCardId { get; set; } - public string Title { get; set; } - public ICollection MenuItems { get; } = new List(); + public Guid MenuCardId { get; set; } = menuCardId; + public string Title { get; set; } = title; + public ICollection MenuItems { get; } = []; public override string ToString() => Title; } diff --git a/2_Libs/EFCore/Tracking/MenusContext.cs b/2_Libs/EFCore/Tracking/MenusContext.cs index 4b9a2f86..b5ef7610 100644 --- a/2_Libs/EFCore/Tracking/MenusContext.cs +++ b/2_Libs/EFCore/Tracking/MenusContext.cs @@ -1,10 +1,7 @@ using Microsoft.EntityFrameworkCore; -class MenusContext : DbContext +class MenusContext(DbContextOptions options) : DbContext(options) { - public MenusContext(DbContextOptions options) - : base(options) {} - public DbSet MenuCards => Set(); public DbSet MenusItems => Set(); diff --git a/2_Libs/EFCore/Tracking/Restaurant.cs b/2_Libs/EFCore/Tracking/Restaurant.cs index 44d6f738..597a170e 100644 --- a/2_Libs/EFCore/Tracking/Restaurant.cs +++ b/2_Libs/EFCore/Tracking/Restaurant.cs @@ -2,9 +2,15 @@ { public Restaurant(string name, Guid id = default) => (_name, _id) = (name, id); - private Guid _id = default; + private readonly Guid _id; + +#pragma warning disable IDE0044 // Add readonly modifier - _name is used with EF Core mapping +#pragma warning disable IDE0032 // Use auto property - _name is used with EF Core mapping private string _name; + public string Name => _name; +#pragma warning restore IDE0032 // Use auto property +#pragma warning restore IDE0044 // Add readonly modifier public override string ToString() => $"{Name}, {_id}"; } diff --git a/2_Libs/EFCore/Tracking/Runner.cs b/2_Libs/EFCore/Tracking/Runner.cs index 2d0f6cb5..96e95e41 100644 --- a/2_Libs/EFCore/Tracking/Runner.cs +++ b/2_Libs/EFCore/Tracking/Runner.cs @@ -1,12 +1,9 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; -class Runner +class Runner(IDbContextFactory menusContextFactory) { - private readonly IDbContextFactory _menusContextFactory; - - public Runner(IDbContextFactory menusContextFactory) => - _menusContextFactory = menusContextFactory; + private readonly IDbContextFactory _menusContextFactory = menusContextFactory; public async Task CreateDatabaseAsync() { @@ -20,8 +17,8 @@ public async Task AddRecordsAsync() using var context = _menusContextFactory.CreateDbContext(); MenuCard soupCard = new("Soups"); - MenuItem[] soups = new[] - { + MenuItem[] soups = + [ new MenuItem("Consommé Célestine (with shredded pancake)") { Price = 4.8m, @@ -37,7 +34,7 @@ public async Task AddRecordsAsync() Price = 4.8m, MenuCard = soupCard } - }; + ]; foreach (var soup in soups) { @@ -70,7 +67,7 @@ public async Task ObjectTrackingAsync() where m.Text.StartsWith("Con") select m).FirstOrDefaultAsync(); var m2 = await (from m in context.MenusItems - where m.Text.Contains("(") + where m.Text.Contains('(') select m).FirstOrDefaultAsync(); if (object.ReferenceEquals(m1, m2)) { diff --git a/2_Libs/EFCore/Tracking/Tracking.csproj b/2_Libs/EFCore/Tracking/Tracking.csproj index a14a098e..4ff49212 100644 --- a/2_Libs/EFCore/Tracking/Tracking.csproj +++ b/2_Libs/EFCore/Tracking/Tracking.csproj @@ -2,19 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/2_Libs/EFCore/Transactions/ColumnNames.cs b/2_Libs/EFCore/Transactions/ColumnNames.cs index d25e8759..fd0d0ebb 100644 --- a/2_Libs/EFCore/Transactions/ColumnNames.cs +++ b/2_Libs/EFCore/Transactions/ColumnNames.cs @@ -1,4 +1,6 @@ -internal class ColumnNames +namespace TransactionsSamples; + +internal class ColumnNames { public const string LastUpdated = nameof(LastUpdated); public const string IsDeleted = nameof(IsDeleted); diff --git a/2_Libs/EFCore/Transactions/GlobalUsings.cs b/2_Libs/EFCore/Transactions/GlobalUsings.cs new file mode 100644 index 00000000..9fff94bf --- /dev/null +++ b/2_Libs/EFCore/Transactions/GlobalUsings.cs @@ -0,0 +1,11 @@ +global using System.Transactions; + +global using Microsoft.EntityFrameworkCore; +global using Microsoft.EntityFrameworkCore.Metadata.Builders; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Hosting; + +global using TransactionsSamples; + +global using static TransactionsSamples.ColumnNames; diff --git a/2_Libs/EFCore/Transactions/MenuCard.cs b/2_Libs/EFCore/Transactions/MenuCard.cs index c5651cea..9a3352a7 100644 --- a/2_Libs/EFCore/Transactions/MenuCard.cs +++ b/2_Libs/EFCore/Transactions/MenuCard.cs @@ -1,10 +1,9 @@ -public class MenuCard -{ - public MenuCard(string title, Guid menuCardId = default) - => (Title, MenuCardId) = (title, menuCardId); +namespace TransactionsSamples; - public Guid MenuCardId { get; set; } - public string Title { get; set; } +public class MenuCard(string title, Guid menuCardId = default) +{ + public Guid MenuCardId { get; set; } = menuCardId; + public string Title { get; set; } = title; public ICollection MenuItems { get; } = new List(); public override string ToString() => Title; } diff --git a/2_Libs/EFCore/Transactions/MenuCardConfiguration.cs b/2_Libs/EFCore/Transactions/MenuCardConfiguration.cs index 1e7943bd..cbf94d6a 100644 --- a/2_Libs/EFCore/Transactions/MenuCardConfiguration.cs +++ b/2_Libs/EFCore/Transactions/MenuCardConfiguration.cs @@ -1,7 +1,4 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -using static ColumnNames; +namespace TransactionsSamples; internal class MenuCardConfiguration : IEntityTypeConfiguration { diff --git a/2_Libs/EFCore/Transactions/MenuItem.cs b/2_Libs/EFCore/Transactions/MenuItem.cs index a37540bf..ac71ec0f 100644 --- a/2_Libs/EFCore/Transactions/MenuItem.cs +++ b/2_Libs/EFCore/Transactions/MenuItem.cs @@ -1,9 +1,9 @@ -public class MenuItem -{ - public MenuItem(string text, Guid menuItemId = default) => (Text, MenuItemId) = (text, menuItemId); +namespace TransactionsSamples; - public Guid MenuItemId { get; set; } - public string Text { get; set; } +public class MenuItem(string text, Guid menuItemId = default) +{ + public Guid MenuItemId { get; set; } = menuItemId; + public string Text { get; set; } = text; public decimal? Price { get; set; } public Guid MenuCardId { get; internal set; } private MenuCard? _menuCard; diff --git a/2_Libs/EFCore/Transactions/MenuItemConfiguration.cs b/2_Libs/EFCore/Transactions/MenuItemConfiguration.cs index edc7b051..77b3d9f1 100644 --- a/2_Libs/EFCore/Transactions/MenuItemConfiguration.cs +++ b/2_Libs/EFCore/Transactions/MenuItemConfiguration.cs @@ -1,7 +1,4 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -using static ColumnNames; +namespace TransactionsSamples; internal class MenuItemConfiguration : IEntityTypeConfiguration { diff --git a/2_Libs/EFCore/Transactions/MenusContext.cs b/2_Libs/EFCore/Transactions/MenusContext.cs index 58e623de..380c6adc 100644 --- a/2_Libs/EFCore/Transactions/MenusContext.cs +++ b/2_Libs/EFCore/Transactions/MenusContext.cs @@ -1,10 +1,7 @@ -using Microsoft.EntityFrameworkCore; +namespace TransactionsSamples; -class MenusContext : DbContext +class MenusContext(DbContextOptions options) : DbContext(options) { - public MenusContext(DbContextOptions options) - : base(options) {} - public DbSet MenuCards => Set(); public DbSet MenuItems => Set(); @@ -29,7 +26,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasData(menus); } - private IEnumerable GetInitialMenuItems(dynamic card, Guid restaurantId) => + private object[] GetInitialMenuItems(dynamic card, Guid restaurantId) => Enumerable.Range(1, 20).Select(id => new { MenuItemId = Guid.NewGuid(), @@ -37,7 +34,7 @@ private IEnumerable GetInitialMenuItems(dynamic card, Guid restaurantId) Price = 6.5M, IsDeleted = false, LastUpdated = DateTime.Now, - MenuCardId = card.MenuCardId, + card.MenuCardId, RestaurantId = restaurantId }).ToArray(); } diff --git a/2_Libs/EFCore/Transactions/Program.cs b/2_Libs/EFCore/Transactions/Program.cs index f9970391..c8213fc6 100644 --- a/2_Libs/EFCore/Transactions/Program.cs +++ b/2_Libs/EFCore/Transactions/Program.cs @@ -1,16 +1,9 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -using var host = Host.CreateDefaultBuilder(args) +using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { - var connectionString = context.Configuration.GetConnectionString("MenusConnection"); - services.AddDbContextFactory(options => - { - options.UseSqlServer(connectionString); - }); + string connectionString = context.Configuration.GetConnectionString("MenusConnection") ?? throw new InvalidOperationException("Could not read MenusConnection"); + services.AddDbContextFactory(options => + options.UseSqlServer(connectionString)); services.AddScoped(); }) diff --git a/2_Libs/EFCore/Transactions/Restaurant.cs b/2_Libs/EFCore/Transactions/Restaurant.cs index 44d6f738..9dfc645c 100644 --- a/2_Libs/EFCore/Transactions/Restaurant.cs +++ b/2_Libs/EFCore/Transactions/Restaurant.cs @@ -1,10 +1,14 @@ -public class Restaurant +namespace TransactionsSamples; + +public class Restaurant(string name, Guid id = default) { - public Restaurant(string name, Guid id = default) => (_name, _id) = (name, id); +#pragma warning disable IDE0032 // Use auto property - fields used in EF Core mapping + private readonly Guid _id = id; - private Guid _id = default; - private string _name; - public string Name => _name; + private readonly string _name = name; + public string Name => + _name; +#pragma warning restore IDE0032 // Use auto property public override string ToString() => $"{Name}, {_id}"; } diff --git a/2_Libs/EFCore/Transactions/RestaurantConfiguration.cs b/2_Libs/EFCore/Transactions/RestaurantConfiguration.cs index eeae4738..730d42db 100644 --- a/2_Libs/EFCore/Transactions/RestaurantConfiguration.cs +++ b/2_Libs/EFCore/Transactions/RestaurantConfiguration.cs @@ -1,5 +1,4 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; +namespace TransactionsSamples; internal class RestaurantConfiguration : IEntityTypeConfiguration { diff --git a/2_Libs/EFCore/Transactions/Runner.cs b/2_Libs/EFCore/Transactions/Runner.cs index d42fadd5..a5c5bf20 100644 --- a/2_Libs/EFCore/Transactions/Runner.cs +++ b/2_Libs/EFCore/Transactions/Runner.cs @@ -1,13 +1,8 @@ -using Microsoft.EntityFrameworkCore; +namespace TransactionsSamples; -using System.Transactions; - -class Runner +class Runner(IDbContextFactory menusContextFactory) { - private readonly IDbContextFactory _menusContextFactory; - - public Runner(IDbContextFactory menusContextFactory) => - _menusContextFactory = menusContextFactory; + private readonly IDbContextFactory _menusContextFactory = menusContextFactory; public async Task CreateDatabaseAsync() { @@ -58,7 +53,7 @@ public async Task AddTwoRecordsWithTwoTxAsync() var card = context.MenuCards .OrderBy(mc => mc.MenuCardId) .First(); - MenuItem m1 = new MenuItem("added") + MenuItem m1 = new("added") { MenuCardId = card.MenuCardId, Price = 99.99m @@ -133,7 +128,8 @@ public async Task AmbientTransactionsAsync() using var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled); - if (Transaction.Current is null) throw new InvalidOperationException("no ambient transaction available"); + if (Transaction.Current is null) + throw new InvalidOperationException("no ambient transaction available"); Transaction.Current.TransactionCompleted += (sender, e) => { var ti = e.Transaction?.TransactionInformation; diff --git a/2_Libs/EFCore/Transactions/Transactions.csproj b/2_Libs/EFCore/Transactions/Transactions.csproj index a14a098e..4ff49212 100644 --- a/2_Libs/EFCore/Transactions/Transactions.csproj +++ b/2_Libs/EFCore/Transactions/Transactions.csproj @@ -2,19 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/3_Web/Services/GRPC/Books.Data/Books.Data.csproj b/3_Web/Services/GRPC/Books.Data/Books.Data.csproj index 8b85ec16..e3400336 100644 --- a/3_Web/Services/GRPC/Books.Data/Books.Data.csproj +++ b/3_Web/Services/GRPC/Books.Data/Books.Data.csproj @@ -1,13 +1,13 @@ - net7.0 + net8.0 enable enable - + diff --git a/3_Web/Services/GRPC/Books.Shared/Books.Shared.csproj b/3_Web/Services/GRPC/Books.Shared/Books.Shared.csproj index 2a808c23..5d6e8277 100644 --- a/3_Web/Services/GRPC/Books.Shared/Books.Shared.csproj +++ b/3_Web/Services/GRPC/Books.Shared/Books.Shared.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable diff --git a/3_Web/Services/GRPC/GRPC.BooksClient/GRPC.BooksClient.csproj b/3_Web/Services/GRPC/GRPC.BooksClient/GRPC.BooksClient.csproj index 3b023b08..211e234d 100644 --- a/3_Web/Services/GRPC/GRPC.BooksClient/GRPC.BooksClient.csproj +++ b/3_Web/Services/GRPC/GRPC.BooksClient/GRPC.BooksClient.csproj @@ -2,19 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/3_Web/Services/GRPC/GRPC.SensorClient/GRPC.SensorClient.csproj b/3_Web/Services/GRPC/GRPC.SensorClient/GRPC.SensorClient.csproj index e109bfac..ead1b8e5 100644 --- a/3_Web/Services/GRPC/GRPC.SensorClient/GRPC.SensorClient.csproj +++ b/3_Web/Services/GRPC/GRPC.SensorClient/GRPC.SensorClient.csproj @@ -2,19 +2,19 @@ Exe - net7.0 + net8.0 enable enable - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/3_Web/Services/GRPC/GRPCService/GRPCService.csproj b/3_Web/Services/GRPC/GRPCService/GRPCService.csproj index fe3327a0..0b12f5c1 100644 --- a/3_Web/Services/GRPC/GRPCService/GRPCService.csproj +++ b/3_Web/Services/GRPC/GRPCService/GRPCService.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable @@ -16,7 +16,7 @@ - + diff --git a/3_Web/Services/GRPC/GRPCService/Program.cs b/3_Web/Services/GRPC/GRPCService/Program.cs index e38ea4bb..229c3d21 100644 --- a/3_Web/Services/GRPC/GRPCService/Program.cs +++ b/3_Web/Services/GRPC/GRPCService/Program.cs @@ -14,13 +14,17 @@ builder.Services.AddGrpc(); builder.Services.AddDbContext(options => { - string connectionString = builder.Configuration.GetConnectionString("BooksConnection"); - if (connectionString is null) throw new InvalidOperationException("Configure the connection string"); + string connectionString = builder.Configuration.GetConnectionString("BooksConnection") ?? throw new InvalidOperationException("Configure the connection string"); options.UseSqlServer(connectionString); }); var app = builder.Build(); +{ + using var scope = app.Services.CreateScope(); + await scope.ServiceProvider.GetRequiredService().Database.EnsureCreatedAsync(); +} + // Configure the HTTP request pipeline. app.MapGrpcService(); app.MapGrpcService(); diff --git a/3_Web/Services/GRPC/GRPCService/appsettings.json b/3_Web/Services/GRPC/GRPCService/appsettings.json index 2972cc9a..df3b1ca0 100644 --- a/3_Web/Services/GRPC/GRPCService/appsettings.json +++ b/3_Web/Services/GRPC/GRPCService/appsettings.json @@ -13,6 +13,6 @@ } }, "ConnectionStrings": { - "BooksConnection": "server=(localdb)\\mssqllocaldb;database=ProCSharpBooksApi12;trusted_connection=true" + "BooksConnection": "server=(localdb)\\mssqllocaldb;database=ProCSharpBooks;trusted_connection=true" } } diff --git a/3_Web/SignalR/SignalRSample/ChatServer/ChatServer.csproj b/3_Web/SignalR/SignalRSample/ChatServer/ChatServer.csproj index 8007e0d9..98ca50d9 100644 --- a/3_Web/SignalR/SignalRSample/ChatServer/ChatServer.csproj +++ b/3_Web/SignalR/SignalRSample/ChatServer/ChatServer.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable enable 329439ca-02ee-452b-8378-e171fa3c5c97 diff --git a/3_Web/SignalR/SignalRSample/WinAppChatClient/ViewModels/ChatViewModel.cs b/3_Web/SignalR/SignalRSample/WinAppChatClient/ViewModels/ChatViewModel.cs index a073bcf6..f913960b 100644 --- a/3_Web/SignalR/SignalRSample/WinAppChatClient/ViewModels/ChatViewModel.cs +++ b/3_Web/SignalR/SignalRSample/WinAppChatClient/ViewModels/ChatViewModel.cs @@ -9,36 +9,20 @@ namespace WindowsAppChatClient.ViewModels; -public class ChatViewModel : ObservableObject +public partial class ChatViewModel(IDialogService dialogService, UrlService urlService) : ObservableObject { - private readonly IDialogService _dialogService; - private readonly UrlService _urlService; - private readonly DispatcherQueue _dispatcherQueue; - public ChatViewModel(IDialogService dialogService, UrlService urlService) - { - _dialogService = dialogService; - _urlService = urlService; - _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - - ConnectCommand = new RelayCommand(OnConnect); - SendCommand = new RelayCommand(OnSendMessage); - } + private readonly IDialogService _dialogService = dialogService; + private readonly UrlService _urlService = urlService; + private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); public string? Name { get; set; } public string? Message { get; set; } + + [ObservableProperty] private string? _infoText; - public string? InfoText - { - get => _infoText; - set => SetProperty(ref _infoText, value); - } + [ObservableProperty] private bool _showInfoBar; - public bool ShowInfoBar - { - get => _showInfoBar; - set => SetProperty(ref _showInfoBar, value); - } private void DisplayInfoBar(string text) { @@ -46,15 +30,12 @@ private void DisplayInfoBar(string text) ShowInfoBar = true; } - public ObservableCollection Messages { get; } = new ObservableCollection(); - - public RelayCommand SendCommand { get; } - - public RelayCommand ConnectCommand { get; } + public ObservableCollection Messages { get; } = []; private HubConnection? _hubConnection; - public async void OnConnect() + [RelayCommand] + internal async Task Connect() { await CloseConnectionAsync(); _hubConnection = new HubConnectionBuilder() @@ -82,7 +63,8 @@ public async void OnConnect() private Task HubConnectionClosed(Exception? arg) => _dialogService.ShowMessageAsync("Hub connection closed"); - public async void OnSendMessage() + [RelayCommand] + internal async Task Send() { if (_hubConnection is null) throw new InvalidOperationException("OnConnect needs to be invoked before OnSendMessage"); try diff --git a/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj b/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj index b16aa58a..bc5f48c8 100644 --- a/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj +++ b/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj @@ -1,17 +1,17 @@  WinExe - net6.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 WinAppChatClient app.manifest x86;x64;arm64 - win10-x86;win10-x64;win10-arm64 + win-x86;win-x64;win-arm64 win10-$(Platform).pubxml true - true enable enable + true @@ -25,16 +25,16 @@ - - - + + + - - - + + + @@ -47,5 +44,4 @@ - - + \ No newline at end of file diff --git a/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/MainWindow.xaml.cs b/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/MainWindow.xaml.cs index e076c1fe..174934f3 100644 --- a/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/MainWindow.xaml.cs +++ b/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/MainWindow.xaml.cs @@ -3,10 +3,6 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Windows.UI.Popups; - -using WinRT.Interop; - namespace IntroXAML; /// @@ -29,7 +25,7 @@ public MainWindow() Title = "Message", Content = "button 2 clicked", PrimaryButtonText = "OK", - XamlRoot = this.Content.XamlRoot + XamlRoot = Content.XamlRoot }; await dlg.ShowAsync(); @@ -47,7 +43,7 @@ private async void OnButtonClick(object sender, RoutedEventArgs e) Title = "Message", Content = "button 1 clicked", PrimaryButtonText = "OK", - XamlRoot = this.Content.XamlRoot + XamlRoot = Content.XamlRoot }; await dlg.ShowAsync(); } diff --git a/4_Apps/Windows/XAMLIntro/MarkupExtensions/MarkupExtensions/MarkupExtensions.csproj b/4_Apps/Windows/XAMLIntro/MarkupExtensions/MarkupExtensions/MarkupExtensions.csproj index 54e3c821..8b51a630 100644 --- a/4_Apps/Windows/XAMLIntro/MarkupExtensions/MarkupExtensions/MarkupExtensions.csproj +++ b/4_Apps/Windows/XAMLIntro/MarkupExtensions/MarkupExtensions/MarkupExtensions.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 MarkupExtensions app.manifest x86;x64;arm64 - win10-x86;win10-x64;win10-arm64 - win10-$(Platform).pubxml + win-x86;win-x64;win-arm64 + win-$(Platform).pubxml true - true + true enable enable @@ -33,8 +33,8 @@ - - + + diff --git a/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj b/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj index 5e8849e3..c583019e 100644 --- a/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj +++ b/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 RoutedEvents app.manifest x86;x64;arm64 - win10-x86;win10-x64;win10-arm64 - win10-$(Platform).pubxml + win-x86;win-x64;win-arm64 + win-$(Platform).pubxml true - true + true enable enable @@ -33,8 +33,8 @@ - - + +