From 8501a44a80a9475f21a84fc7a2ec86a8e9d27e15 Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Mon, 11 Dec 2023 04:40:23 +0100 Subject: [PATCH 1/9] .net 8 ef core --- 2_Libs/EFCore/BooksLib/Book.cs | 14 ++---- 2_Libs/EFCore/BooksLib/BooksContext.cs | 5 +- 2_Libs/EFCore/BooksLib/BooksLib.csproj | 6 +-- .../EFCore/ConflictHandling-FirstWins/Book.cs | 15 ++---- .../BooksContext.cs | 9 ++-- .../ConflictHandling-FirstWins.csproj | 10 ++-- .../GlobalUsings.cs | 7 +++ .../ConflictHandling-FirstWins/Program.cs | 14 ++---- .../ConflictHandling-FirstWins/Runner.cs | 21 +++++---- .../EFCore/ConflictHandling-LastWins/Book.cs | 16 ++----- .../ConflictHandling-LastWins/BooksContext.cs | 9 ++-- .../ConflictHandling-LastWins.csproj | 10 ++-- .../ConflictHandling-LastWins/GlobalUsings.cs | 8 ++++ .../ConflictHandling-LastWins/Program.cs | 8 +--- .../ConflictHandling-LastWins/Runner.cs | 16 +++---- 2_Libs/EFCore/Cosmos/Cosmos.csproj | 12 ++--- 2_Libs/EFCore/Cosmos/MenuItem.cs | 8 ++-- 2_Libs/EFCore/Intro/Book.cs | 14 ++---- 2_Libs/EFCore/Intro/BooksContext.cs | 5 +- 2_Libs/EFCore/Intro/Intro.csproj | 10 ++-- 2_Libs/EFCore/Intro/Runner.cs | 8 +--- 2_Libs/EFCore/LoadingRelatedData/Book.cs | 14 ++---- .../EFCore/LoadingRelatedData/BooksContext.cs | 27 +++++------ 2_Libs/EFCore/LoadingRelatedData/Chapter.cs | 11 ++--- .../LoadingRelatedData.csproj | 12 ++--- 2_Libs/EFCore/LoadingRelatedData/Person.cs | 15 ++---- 2_Libs/EFCore/LoadingRelatedData/Runner.cs | 9 ++-- .../EFCore/MigrationApp/MigrationApp.csproj | 6 +-- 2_Libs/EFCore/Models/MenuCard.cs | 9 ++-- 2_Libs/EFCore/Models/MenuItem.cs | 8 ++-- 2_Libs/EFCore/Models/MenusContext.cs | 13 ++---- 2_Libs/EFCore/Models/Models.csproj | 10 ++-- 2_Libs/EFCore/Models/Restaurant.cs | 7 ++- 2_Libs/EFCore/Models/Runner.cs | 11 ++--- .../EFCore/Queries/CompiledQueryExtensions.cs | 10 +--- 2_Libs/EFCore/Queries/MenuCard.cs | 9 ++-- 2_Libs/EFCore/Queries/MenuItem.cs | 8 ++-- 2_Libs/EFCore/Queries/MenusContext.cs | 13 ++---- 2_Libs/EFCore/Queries/Program.cs | 2 - 2_Libs/EFCore/Queries/Queries.csproj | 8 ++-- 2_Libs/EFCore/Queries/Restaurant.cs | 7 ++- 2_Libs/EFCore/Queries/Runner.cs | 5 +- .../EFCore/Relationships/Bank/BankContext.cs | 5 +- .../EFCore/Relationships/Bank/BankRunner.cs | 6 +-- 2_Libs/EFCore/Relationships/Bank/Payments.cs | 18 ++------ 2_Libs/EFCore/Relationships/Books/Book.cs | 15 ++---- .../Relationships/Books/BooksContext.cs | 5 +- .../EFCore/Relationships/Books/BooksRunner.cs | 46 +++++++++---------- 2_Libs/EFCore/Relationships/Books/Person.cs | 18 ++------ 2_Libs/EFCore/Relationships/Menus/MenuItem.cs | 11 ++--- .../Relationships/Menus/MenusContext.cs | 5 +- .../EFCore/Relationships/Menus/MenusRunner.cs | 10 ++-- .../EFCore/Relationships/Relationships.csproj | 14 ++---- .../ScaffoldSample/ScaffoldSample.csproj | 4 +- 2_Libs/EFCore/Tracking/MenuCard.cs | 11 ++--- 2_Libs/EFCore/Tracking/MenusContext.cs | 5 +- 2_Libs/EFCore/Tracking/Restaurant.cs | 8 +++- 2_Libs/EFCore/Tracking/Runner.cs | 15 +++--- 2_Libs/EFCore/Tracking/Tracking.csproj | 10 ++-- 2_Libs/EFCore/Transactions/ColumnNames.cs | 4 +- 2_Libs/EFCore/Transactions/GlobalUsings.cs | 11 +++++ 2_Libs/EFCore/Transactions/MenuCard.cs | 11 ++--- .../Transactions/MenuCardConfiguration.cs | 5 +- 2_Libs/EFCore/Transactions/MenuItem.cs | 10 ++-- .../Transactions/MenuItemConfiguration.cs | 5 +- 2_Libs/EFCore/Transactions/MenusContext.cs | 11 ++--- 2_Libs/EFCore/Transactions/Program.cs | 15 ++---- 2_Libs/EFCore/Transactions/Restaurant.cs | 14 ++++-- .../Transactions/RestaurantConfiguration.cs | 3 +- 2_Libs/EFCore/Transactions/Runner.cs | 16 +++---- .../EFCore/Transactions/Transactions.csproj | 10 ++-- 71 files changed, 310 insertions(+), 450 deletions(-) create mode 100644 2_Libs/EFCore/ConflictHandling-FirstWins/GlobalUsings.cs create mode 100644 2_Libs/EFCore/ConflictHandling-LastWins/GlobalUsings.cs create mode 100644 2_Libs/EFCore/Transactions/GlobalUsings.cs 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 - - + + From 7b793f057f4813710bc416cf4e0f61a5920cf50d Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Thu, 25 Jan 2024 08:06:28 +0100 Subject: [PATCH 2/9] GRPC sample update .NET 8 #203 --- 3_Web/Services/GRPC/Books.Data/Books.Data.csproj | 4 ++-- 3_Web/Services/GRPC/Books.Shared/Books.Shared.csproj | 2 +- .../GRPC/GRPC.BooksClient/GRPC.BooksClient.csproj | 10 +++++----- .../GRPC/GRPC.SensorClient/GRPC.SensorClient.csproj | 10 +++++----- 3_Web/Services/GRPC/GRPCService/GRPCService.csproj | 4 ++-- 3_Web/Services/GRPC/GRPCService/Program.cs | 8 ++++++-- 3_Web/Services/GRPC/GRPCService/appsettings.json | 2 +- 7 files changed, 22 insertions(+), 18 deletions(-) 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" } } From 2d23f02bcdd627480230cb8fff677a1c81adfd4d Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Thu, 25 Jan 2024 08:14:10 +0100 Subject: [PATCH 3/9] SignalR #203 Chapter 28 --- .../SignalRSample/ChatServer/ChatServer.csproj | 2 +- .../WinAppChatClient/WinAppChatClient.csproj | 16 ++++++++-------- .../SignalRStreaming/SignalRStreaming.csproj | 2 +- 3_Web/SignalR/StreamingClient/Program.cs | 4 +++- .../StreamingClient/StreamingClient.csproj | 6 +++--- 5 files changed, 16 insertions(+), 14 deletions(-) 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/WinAppChatClient.csproj b/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj index b16aa58a..b52b05c7 100644 --- a/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj +++ b/3_Web/SignalR/SignalRSample/WinAppChatClient/WinAppChatClient.csproj @@ -1,12 +1,12 @@  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 @@ -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..655247f7 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 diff --git a/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj b/4_Apps/Windows/XAMLIntro/RoutedEvents/RoutedEvents/RoutedEvents.csproj index 5e8849e3..362f785c 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 + win-x86;win-x64;win-arm64 win10-$(Platform).pubxml true - true + true enable enable From 20e0d1768becac15d5f67e6ad54746df22b2bddc Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Sun, 9 Feb 2025 10:08:05 +0100 Subject: [PATCH 7/9] .net 9 styles --- 4_Apps/Styles/Animations/Animations.csproj | 12 +++++----- .../Animations/EasingChartControl.xaml.cs | 22 ++++++++++++------- 4_Apps/Styles/Brushes/Brushes.csproj | 12 +++++----- 4_Apps/Styles/Geometries/Geometries.csproj | 12 +++++----- 4_Apps/Styles/Models/CountryRepository.cs | 6 ++--- 4_Apps/Styles/Models/Models.csproj | 2 +- 4_Apps/Styles/Shapes/MainWindow.xaml.cs | 13 +++++++---- 4_Apps/Styles/Shapes/Shapes.csproj | 12 +++++----- 4_Apps/Styles/StylesAndResources/App.xaml.cs | 6 ++--- .../StylesAndResources.csproj | 12 +++++----- 4_Apps/Styles/Templates/Templates.csproj | 13 ++++++----- .../Views/StyledListUserControl.xaml.cs | 3 +-- .../Transformations/Transformations.csproj | 12 +++++----- 4_Apps/Styles/Transitions/Transitions.csproj | 12 +++++----- .../Views/ListItemsUserControl.xaml.cs | 2 +- .../Styles/VisualStates/VisualStates.csproj | 12 +++++----- 16 files changed, 87 insertions(+), 76 deletions(-) diff --git a/4_Apps/Styles/Animations/Animations.csproj b/4_Apps/Styles/Animations/Animations.csproj index 3bddc0bd..899fa381 100644 --- a/4_Apps/Styles/Animations/Animations.csproj +++ b/4_Apps/Styles/Animations/Animations.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Animations 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/Animations/EasingChartControl.xaml.cs b/4_Apps/Styles/Animations/EasingChartControl.xaml.cs index 80d3feb3..2f57c5a0 100644 --- a/4_Apps/Styles/Animations/EasingChartControl.xaml.cs +++ b/4_Apps/Styles/Animations/EasingChartControl.xaml.cs @@ -16,24 +16,30 @@ public void Draw(EasingFunctionBase easingFunction) { canvas1.Children.Clear(); - PathSegmentCollection pathSegments = new(); + PathSegmentCollection pathSegments = []; for (double i = 0; i < 1; i += SamplingInterval) { double x = i * canvas1.Width; double y = easingFunction.Ease(i) * canvas1.Height; - LineSegment segment = new(); - segment.Point = new Point(x, y); + LineSegment segment = new() + { + Point = new Point(x, y) + }; pathSegments.Add(segment); } - XamlShapes.Path p = new(); - p.Stroke = new SolidColorBrush(Colors.Black); - p.StrokeThickness = 3; - PathFigureCollection figures = new(); - figures.Add(new PathFigure { Segments = pathSegments }); + XamlShapes.Path p = new() + { + Stroke = new SolidColorBrush(Colors.Black), + StrokeThickness = 3 + }; + PathFigureCollection figures = + [ + new PathFigure { Segments = pathSegments } + ]; p.Data = new PathGeometry { Figures = figures }; canvas1.Children.Add(p); } diff --git a/4_Apps/Styles/Brushes/Brushes.csproj b/4_Apps/Styles/Brushes/Brushes.csproj index 934123a5..7974a1c9 100644 --- a/4_Apps/Styles/Brushes/Brushes.csproj +++ b/4_Apps/Styles/Brushes/Brushes.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Brushes 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/Geometries/Geometries.csproj b/4_Apps/Styles/Geometries/Geometries.csproj index 7f42ac89..87d9e933 100644 --- a/4_Apps/Styles/Geometries/Geometries.csproj +++ b/4_Apps/Styles/Geometries/Geometries.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Geometries 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/Models/CountryRepository.cs b/4_Apps/Styles/Models/CountryRepository.cs index 3f4f0736..d7431ccb 100644 --- a/4_Apps/Styles/Models/CountryRepository.cs +++ b/4_Apps/Styles/Models/CountryRepository.cs @@ -4,11 +4,11 @@ public sealed class CountryRepository { private static IEnumerable? s_countries; - public IEnumerable GetCountries() => s_countries ??= new List - { + public IEnumerable GetCountries() => s_countries ??= + [ new() { Name = "Austria", ImagePath = "/Images/Austria.bmp" }, new() { Name = "Germany", ImagePath = "/Images/Germany.bmp" }, new() { Name = "Norway", ImagePath = "/Images/Norway.bmp" }, new() { Name = "USA", ImagePath = "/Images/USA.bmp" } - }; + ]; } diff --git a/4_Apps/Styles/Models/Models.csproj b/4_Apps/Styles/Models/Models.csproj index 2a808c23..5d6e8277 100644 --- a/4_Apps/Styles/Models/Models.csproj +++ b/4_Apps/Styles/Models/Models.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable diff --git a/4_Apps/Styles/Shapes/MainWindow.xaml.cs b/4_Apps/Styles/Shapes/MainWindow.xaml.cs index e0890751..7f368794 100644 --- a/4_Apps/Styles/Shapes/MainWindow.xaml.cs +++ b/4_Apps/Styles/Shapes/MainWindow.xaml.cs @@ -22,8 +22,11 @@ public void SetMouth() { int index = _laugh ? 0 : 1; - PathFigure figure = new() { StartPoint = _mouthPoints[index, 0] }; - figure.Segments = new PathSegmentCollection(); + PathFigure figure = new() + { + StartPoint = _mouthPoints[index, 0], + Segments = [] + }; QuadraticBezierSegment segment1 = new() { Point1 = _mouthPoints[index, 1], @@ -31,8 +34,10 @@ public void SetMouth() }; figure.Segments.Add(segment1); - PathGeometry geometry = new(); - geometry.Figures = new PathFigureCollection(); + PathGeometry geometry = new() + { + Figures = [] + }; geometry.Figures.Add(figure); mouth.Data = geometry; diff --git a/4_Apps/Styles/Shapes/Shapes.csproj b/4_Apps/Styles/Shapes/Shapes.csproj index 5b783f7a..b090b2cf 100644 --- a/4_Apps/Styles/Shapes/Shapes.csproj +++ b/4_Apps/Styles/Shapes/Shapes.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Shapes 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/StylesAndResources/App.xaml.cs b/4_Apps/Styles/StylesAndResources/App.xaml.cs index 310ed2c0..73893eba 100644 --- a/4_Apps/Styles/StylesAndResources/App.xaml.cs +++ b/4_Apps/Styles/StylesAndResources/App.xaml.cs @@ -8,9 +8,9 @@ public partial class App : Application protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) { - m_window = new MainWindow(); - m_window.Activate(); + _window = new MainWindow(); + _window.Activate(); } - private Window? m_window; + private Window? _window; } diff --git a/4_Apps/Styles/StylesAndResources/StylesAndResources.csproj b/4_Apps/Styles/StylesAndResources/StylesAndResources.csproj index a1d96de4..b6efe8c2 100644 --- a/4_Apps/Styles/StylesAndResources/StylesAndResources.csproj +++ b/4_Apps/Styles/StylesAndResources/StylesAndResources.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 StylesAndResources 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/Templates/Templates.csproj b/4_Apps/Styles/Templates/Templates.csproj index 8fb524af..d611ac87 100644 --- a/4_Apps/Styles/Templates/Templates.csproj +++ b/4_Apps/Styles/Templates/Templates.csproj @@ -1,17 +1,18 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Template 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 + true @@ -25,8 +26,8 @@ - - + + diff --git a/4_Apps/Styles/Templates/Views/StyledListUserControl.xaml.cs b/4_Apps/Styles/Templates/Views/StyledListUserControl.xaml.cs index 0e9179e3..abb78661 100644 --- a/4_Apps/Styles/Templates/Views/StyledListUserControl.xaml.cs +++ b/4_Apps/Styles/Templates/Views/StyledListUserControl.xaml.cs @@ -9,8 +9,7 @@ namespace Templates.Views { public sealed partial class StyledListUserControl : UserControl { - public ObservableCollection Countries { get; } = - new ObservableCollection(); + public ObservableCollection Countries { get; } =[]; public StyledListUserControl() { diff --git a/4_Apps/Styles/Transformations/Transformations.csproj b/4_Apps/Styles/Transformations/Transformations.csproj index 4430313b..6f8a771d 100644 --- a/4_Apps/Styles/Transformations/Transformations.csproj +++ b/4_Apps/Styles/Transformations/Transformations.csproj @@ -2,15 +2,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Transformations 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 @@ -26,8 +26,8 @@ - - + + diff --git a/4_Apps/Styles/Transitions/Transitions.csproj b/4_Apps/Styles/Transitions/Transitions.csproj index 40d42936..9210307d 100644 --- a/4_Apps/Styles/Transitions/Transitions.csproj +++ b/4_Apps/Styles/Transitions/Transitions.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 Transitions 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Styles/Transitions/Views/ListItemsUserControl.xaml.cs b/4_Apps/Styles/Transitions/Views/ListItemsUserControl.xaml.cs index ae5d4788..9eafef51 100644 --- a/4_Apps/Styles/Transitions/Views/ListItemsUserControl.xaml.cs +++ b/4_Apps/Styles/Transitions/Views/ListItemsUserControl.xaml.cs @@ -17,7 +17,7 @@ private void OnAdd(object sender, RoutedEventArgs e) } private Rectangle CreateRectangle() => - new Rectangle + new() { Width = 90, Height = 40, diff --git a/4_Apps/Styles/VisualStates/VisualStates.csproj b/4_Apps/Styles/VisualStates/VisualStates.csproj index fbdc4763..b1828cd1 100644 --- a/4_Apps/Styles/VisualStates/VisualStates.csproj +++ b/4_Apps/Styles/VisualStates/VisualStates.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 VisualStates 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 @@ -25,8 +25,8 @@ - - + + From e0035d0af014bf1cb43a554fbee2ccb54518db31 Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Sun, 9 Feb 2025 10:08:42 +0100 Subject: [PATCH 8/9] .net 8 patterns winui --- 4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj | 14 +++++++------- .../Patterns/BooksApp/BooksApp/MainWindow.xaml.cs | 4 ++-- .../BooksApp/Views/BookDetailUserControl.xaml.cs | 2 +- .../BooksApp/Views/BookItemUserControl.xaml.cs | 8 ++------ 4_Apps/Patterns/BooksLib/BooksLib.csproj | 6 +++--- 4_Apps/Patterns/BooksLib/Models/Book.cs | 15 +++++---------- .../GenericViewModels/GenericViewModels.csproj | 4 ++-- .../GenericViewModels/ViewModels/ItemViewModel.cs | 5 ++--- 8 files changed, 24 insertions(+), 34 deletions(-) diff --git a/4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj b/4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj index 1f4e88fe..93843b21 100644 --- a/4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj +++ b/4_Apps/Patterns/BooksApp/BooksApp/BooksApp.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 BooksApp 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 @@ -30,9 +30,9 @@ - - - + + + diff --git a/4_Apps/Patterns/BooksApp/BooksApp/MainWindow.xaml.cs b/4_Apps/Patterns/BooksApp/BooksApp/MainWindow.xaml.cs index a277a5fe..ccfefc23 100644 --- a/4_Apps/Patterns/BooksApp/BooksApp/MainWindow.xaml.cs +++ b/4_Apps/Patterns/BooksApp/BooksApp/MainWindow.xaml.cs @@ -4,8 +4,8 @@ public sealed partial class MainWindow : Window { public MainWindow() { - this.InitializeComponent(); - this.SizeChanged += OnSizeChanged; + InitializeComponent(); + SizeChanged += OnSizeChanged; ViewModel = Ioc.Default.GetRequiredService(); ViewModel.SetNavigationFrame(MainFrame); } diff --git a/4_Apps/Patterns/BooksApp/BooksApp/Views/BookDetailUserControl.xaml.cs b/4_Apps/Patterns/BooksApp/BooksApp/Views/BookDetailUserControl.xaml.cs index c6a9e014..7e0c3109 100644 --- a/4_Apps/Patterns/BooksApp/BooksApp/Views/BookDetailUserControl.xaml.cs +++ b/4_Apps/Patterns/BooksApp/BooksApp/Views/BookDetailUserControl.xaml.cs @@ -4,7 +4,7 @@ public sealed partial class BookDetailUserControl : UserControl { public BookDetailUserControl() { - this.InitializeComponent(); + InitializeComponent(); } public BookDetailViewModel ViewModel diff --git a/4_Apps/Patterns/BooksApp/BooksApp/Views/BookItemUserControl.xaml.cs b/4_Apps/Patterns/BooksApp/BooksApp/Views/BookItemUserControl.xaml.cs index f6599522..48018d65 100644 --- a/4_Apps/Patterns/BooksApp/BooksApp/Views/BookItemUserControl.xaml.cs +++ b/4_Apps/Patterns/BooksApp/BooksApp/Views/BookItemUserControl.xaml.cs @@ -1,8 +1,4 @@ -using BooksLib.ViewModels; - -using Microsoft.UI.Input; -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Input; using Microsoft.UI.Xaml.Input; // To learn more about WinUI, the WinUI project structure, @@ -14,7 +10,7 @@ public sealed partial class BookItemUserControl : UserControl { public BookItemUserControl() { - this.InitializeComponent(); + InitializeComponent(); } public BookItemViewModel BookItemViewModel diff --git a/4_Apps/Patterns/BooksLib/BooksLib.csproj b/4_Apps/Patterns/BooksLib/BooksLib.csproj index 4a8701fc..6c582b7a 100644 --- a/4_Apps/Patterns/BooksLib/BooksLib.csproj +++ b/4_Apps/Patterns/BooksLib/BooksLib.csproj @@ -1,14 +1,14 @@  - net7.0 + net8.0 enable enable - - + + diff --git a/4_Apps/Patterns/BooksLib/Models/Book.cs b/4_Apps/Patterns/BooksLib/Models/Book.cs index 0c5866ee..ec190aeb 100644 --- a/4_Apps/Patterns/BooksLib/Models/Book.cs +++ b/4_Apps/Patterns/BooksLib/Models/Book.cs @@ -2,23 +2,18 @@ namespace BooksLib.Models; -public class Book : ObservableObject +public class Book(string? title = null, string? publisher = null, int id = 0) : + ObservableObject { - public Book(string? title = null, string? publisher = null, int id = 0) - { - BookId = id; - _title = title ?? string.Empty; - _publisher = publisher ?? string.Empty; - } - public int BookId { get; set; } - private string _title; + public int BookId { get; set; } = id; + private string _title = title ?? string.Empty; public string Title { get => _title; set => SetProperty(ref _title, value); } - private string _publisher; + private string _publisher = publisher ?? string.Empty; public string Publisher { get => _publisher; diff --git a/4_Apps/Patterns/GenericViewModels/GenericViewModels.csproj b/4_Apps/Patterns/GenericViewModels/GenericViewModels.csproj index 4fc864e4..5bb7373d 100644 --- a/4_Apps/Patterns/GenericViewModels/GenericViewModels.csproj +++ b/4_Apps/Patterns/GenericViewModels/GenericViewModels.csproj @@ -1,13 +1,13 @@  - net7.0 + net8.0 enable enable - + diff --git a/4_Apps/Patterns/GenericViewModels/ViewModels/ItemViewModel.cs b/4_Apps/Patterns/GenericViewModels/ViewModels/ItemViewModel.cs index 09f5ca61..247d276e 100644 --- a/4_Apps/Patterns/GenericViewModels/ViewModels/ItemViewModel.cs +++ b/4_Apps/Patterns/GenericViewModels/ViewModels/ItemViewModel.cs @@ -1,9 +1,8 @@ namespace GenericViewModels.ViewModels; -public abstract class ItemViewModel : ViewModelBase, IItemViewModel +public abstract class ItemViewModel(T? item) : ViewModelBase, IItemViewModel { - public ItemViewModel(T? item) => _item = item; - private T? _item; + private T? _item = item; public virtual T? Item { get => _item; From 9e3a3e6d4feb580923ac4601351ebca838ade83b Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Sun, 9 Feb 2025 10:09:47 +0100 Subject: [PATCH 9/9] .net 8 winui --- .../ControlsSamples/ControlsSamples.csproj | 12 ++++++------ .../ControlsSamples/Views/ButtonsPage.xaml.cs | 6 +----- .../Views/DateSelectionPage.xaml.cs | 11 ++++------- .../Views/RangeControlsPage.xaml.cs | 6 ++++-- .../DataBindingSamples.csproj | 13 +++++++------ .../DataBindingSamples/Models/Book.cs | 18 +++++------------- .../Services/BooksService.cs | 2 +- .../Services/SampleBooksService.cs | 6 +++--- .../Utilities/BookTemplateSelector.cs | 2 +- .../Utilities/CollectionToStringConverter.cs | 2 +- .../LayoutSamples/LayoutSamples.csproj | 8 ++++---- .../NavigationControls.csproj | 12 ++++++------ .../AttachedProperty/AttachedProperty.csproj | 4 ++-- .../CustomMarkupExtensions.csproj | 6 +++--- .../DependencyObjectSample.csproj | 4 ++-- .../HelloWindows/HelloWindows.csproj | 4 ++-- .../XAMLIntro/Intro/Intro/Intro/Intro.csproj | 4 ++-- .../MarkupExtensions/MarkupExtensions.csproj | 4 ++-- .../RoutedEvents/RoutedEvents.csproj | 6 +++--- 19 files changed, 59 insertions(+), 71 deletions(-) diff --git a/4_Apps/Windows/ControlsSamples/ControlsSamples/ControlsSamples.csproj b/4_Apps/Windows/ControlsSamples/ControlsSamples/ControlsSamples.csproj index 0a80fe83..fb0b8e3e 100644 --- a/4_Apps/Windows/ControlsSamples/ControlsSamples/ControlsSamples.csproj +++ b/4_Apps/Windows/ControlsSamples/ControlsSamples/ControlsSamples.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 ControlsSamples 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/ButtonsPage.xaml.cs b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/ButtonsPage.xaml.cs index 15addf58..c294592e 100644 --- a/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/ButtonsPage.xaml.cs +++ b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/ButtonsPage.xaml.cs @@ -1,10 +1,6 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Windows.UI.Popups; - -using WinRT.Interop; - namespace ControlsSamples.Views { public sealed partial class ButtonsPage : Page @@ -18,7 +14,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/ControlsSamples/ControlsSamples/Views/DateSelectionPage.xaml.cs b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/DateSelectionPage.xaml.cs index 6e89531f..f95afdb4 100644 --- a/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/DateSelectionPage.xaml.cs +++ b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/DateSelectionPage.xaml.cs @@ -4,9 +4,6 @@ using System.Globalization; using Windows.UI; -using Windows.UI.Popups; - -using WinRT.Interop; namespace ControlsSamples.Views; @@ -60,7 +57,7 @@ async void SetBookings() var booking = bookings.SingleOrDefault(b => b.day.Date == item.Date.Date); if (booking.bookings > 0) { - List colors = new(); + List colors = []; for (int i = 0; i < booking.bookings; i++) { if (item.Date.DayOfWeek == DayOfWeek.Saturday || item.Date.DayOfWeek == DayOfWeek.Sunday) @@ -80,8 +77,8 @@ async void SetBookings() private IEnumerable<(DateTimeOffset day, int bookings)> GetBookings() { - int[] bookingDays = { 2, 3, 5, 8, 12, 13, 18, 21, 23, 27 }; - int[] bookingsPerDay = { 1, 4, 3, 6, 4, 5, 1, 3, 1, 1 }; + int[] bookingDays = [2, 3, 5, 8, 12, 13, 18, 21, 23, 27]; + int[] bookingsPerDay = [1, 4, 3, 6, 4, 5, 1, 3, 1, 1]; for (int i = 0; i < 10; i++) { @@ -89,7 +86,7 @@ async void SetBookings() } } - private readonly List currentDatesSelected = new(); + private readonly List currentDatesSelected = []; private async void OnDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args) { diff --git a/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/RangeControlsPage.xaml.cs b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/RangeControlsPage.xaml.cs index 2db1109e..16020126 100644 --- a/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/RangeControlsPage.xaml.cs +++ b/4_Apps/Windows/ControlsSamples/ControlsSamples/Views/RangeControlsPage.xaml.cs @@ -13,8 +13,10 @@ public RangeControlsPage() private void ShowProgress() { - DispatcherTimer timer = new(); - timer.Interval = TimeSpan.FromSeconds(1); + DispatcherTimer timer = new() + { + Interval = TimeSpan.FromSeconds(1) + }; int i = 0; timer.Tick += (sender, e) => progressBar1.Value = i++ % 100; diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/DataBindingSamples.csproj b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/DataBindingSamples.csproj index 9e40480e..580bab4b 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/DataBindingSamples.csproj +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/DataBindingSamples.csproj @@ -1,17 +1,18 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 DataBindingSamples 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 + true @@ -25,8 +26,8 @@ - - + + diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Models/Book.cs b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Models/Book.cs index ad0ee034..1cd74f8c 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Models/Book.cs +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Models/Book.cs @@ -1,17 +1,9 @@ namespace DataBindingSamples.Models; -public class Book : ObservableObject +public partial class Book(int id, string title, string publisher, params string[] authors) : ObservableObject { - public Book(int id, string title, string publisher, params string[] authors) - { - BookId = id; - _title = title; - _publisher = publisher; - Authors = authors; - } - - public int BookId { get; } - private string _title; + public int BookId { get; } = id; + private string _title = title; public string Title { @@ -19,13 +11,13 @@ public string Title set => SetProperty(ref _title, value); } - private string _publisher; + private string _publisher = publisher; public string Publisher { get => _publisher; set => SetProperty(ref _publisher, value); } - public IEnumerable Authors { get; } + public IEnumerable Authors { get; } = authors; public override string ToString() => Title; } diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/BooksService.cs b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/BooksService.cs index 71c16e22..b21c4e79 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/BooksService.cs +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/BooksService.cs @@ -6,7 +6,7 @@ namespace DataBindingSamples.Services; public class BooksService { - private readonly ObservableCollection _books = new(); + private readonly ObservableCollection _books = []; public void RefreshBooks() { diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/SampleBooksService.cs b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/SampleBooksService.cs index 77a927fe..0b89e432 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/SampleBooksService.cs +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Services/SampleBooksService.cs @@ -4,13 +4,13 @@ namespace DataBindingSamples.Services; public class SampleBooksService { - private readonly List _books = new() - { + private readonly List _books = + [ new(1, "Professional C# and .NET - 2021 Edition", "Wrox Press", "Christian Nagel"), new(2, "Professional C# 7 and .NET Core 2", "Wrox Press", "Christian Nagel"), new(3, "Professional C# 6 and .NET Core 1.0", "Wrox Press", "Christian Nagel"), new(4, "Professional C# 5.0 and .NET 4.5.1", "Wrox Press", "Christian Nagel", "Jay Glynn", "Morgan Skinner"), new(5, "Enterprise Services with the .NET Framework", "AWL", "Christian Nagel") - }; + ]; public IEnumerable GetSampleBooks() => _books; } diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/BookTemplateSelector.cs b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/BookTemplateSelector.cs index 364728ae..91c4a32f 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/BookTemplateSelector.cs +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/BookTemplateSelector.cs @@ -4,7 +4,7 @@ namespace DataBindingSamples.Utilities; -public class BookTemplateSelector : DataTemplateSelector +public partial class BookTemplateSelector : DataTemplateSelector { public DataTemplate? WroxTemplate { get; set; } public DataTemplate? DefaultTemplate { get; set; } diff --git a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/CollectionToStringConverter.cs b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/CollectionToStringConverter.cs index bcbaed3f..fdf0397d 100644 --- a/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/CollectionToStringConverter.cs +++ b/4_Apps/Windows/DataBindingSamples/DataBindingSamples/Utilities/CollectionToStringConverter.cs @@ -2,7 +2,7 @@ namespace DataBindingSamples.Utilities; -public class CollectionToStringConverter : IValueConverter +public partial class CollectionToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { diff --git a/4_Apps/Windows/LayoutSamples/LayoutSamples/LayoutSamples.csproj b/4_Apps/Windows/LayoutSamples/LayoutSamples/LayoutSamples.csproj index 51c3d2ed..e4d46f7c 100644 --- a/4_Apps/Windows/LayoutSamples/LayoutSamples/LayoutSamples.csproj +++ b/4_Apps/Windows/LayoutSamples/LayoutSamples/LayoutSamples.csproj @@ -1,15 +1,15 @@ WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 LayoutSamples 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 diff --git a/4_Apps/Windows/NavigationControls/NavigationControls/NavigationControls.csproj b/4_Apps/Windows/NavigationControls/NavigationControls/NavigationControls.csproj index 95f92707..53b8abcb 100644 --- a/4_Apps/Windows/NavigationControls/NavigationControls/NavigationControls.csproj +++ b/4_Apps/Windows/NavigationControls/NavigationControls/NavigationControls.csproj @@ -1,15 +1,15 @@  WinExe - net7.0-windows10.0.19041.0 + net8.0-windows10.0.19041.0 10.0.17763.0 NavigationControls 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 @@ -25,8 +25,8 @@ - - + + diff --git a/4_Apps/Windows/XAMLIntro/AttachedProperty/AttachedProperty/AttachedProperty.csproj b/4_Apps/Windows/XAMLIntro/AttachedProperty/AttachedProperty/AttachedProperty.csproj index bd18fa31..a19d7e54 100644 --- a/4_Apps/Windows/XAMLIntro/AttachedProperty/AttachedProperty/AttachedProperty.csproj +++ b/4_Apps/Windows/XAMLIntro/AttachedProperty/AttachedProperty/AttachedProperty.csproj @@ -23,8 +23,8 @@ - - + + diff --git a/4_Apps/Windows/XAMLIntro/CustomMarkupExtensions/CustomMarkupExtensions/CustomMarkupExtensions.csproj b/4_Apps/Windows/XAMLIntro/CustomMarkupExtensions/CustomMarkupExtensions/CustomMarkupExtensions.csproj index 7b4ff89c..5457b35f 100644 --- a/4_Apps/Windows/XAMLIntro/CustomMarkupExtensions/CustomMarkupExtensions/CustomMarkupExtensions.csproj +++ b/4_Apps/Windows/XAMLIntro/CustomMarkupExtensions/CustomMarkupExtensions/CustomMarkupExtensions.csproj @@ -9,7 +9,7 @@ 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/DependencyObjectSample/DependencyObjectSample/DependencyObjectSample.csproj b/4_Apps/Windows/XAMLIntro/DependencyObjectSample/DependencyObjectSample/DependencyObjectSample.csproj index a7e19e68..c5191fe0 100644 --- a/4_Apps/Windows/XAMLIntro/DependencyObjectSample/DependencyObjectSample/DependencyObjectSample.csproj +++ b/4_Apps/Windows/XAMLIntro/DependencyObjectSample/DependencyObjectSample/DependencyObjectSample.csproj @@ -33,8 +33,8 @@ - - + + diff --git a/4_Apps/Windows/XAMLIntro/HelloWindows/HelloWindows/HelloWindows.csproj b/4_Apps/Windows/XAMLIntro/HelloWindows/HelloWindows/HelloWindows.csproj index aacfb916..c9f1d529 100644 --- a/4_Apps/Windows/XAMLIntro/HelloWindows/HelloWindows/HelloWindows.csproj +++ b/4_Apps/Windows/XAMLIntro/HelloWindows/HelloWindows/HelloWindows.csproj @@ -33,8 +33,8 @@ - - + + diff --git a/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/Intro.csproj b/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/Intro.csproj index 49c0b83f..71b6c675 100644 --- a/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/Intro.csproj +++ b/4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/Intro.csproj @@ -31,8 +31,8 @@ - - + +