diff --git a/docs/errata/README.md b/docs/errata/README.md index cc43256..c024809 100644 --- a/docs/errata/README.md +++ b/docs/errata/README.md @@ -2,7 +2,7 @@ If you find any mistakes in the seventh edition, *C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals*, or if you have suggestions for improvements, then please [raise an issue in this repository](https://github.com/markjprice/cs11dotnet7/issues) or email me at markjprice (at) gmail.com. -[**Errata** (38 items)](errata.md): Typos, tool user interface changes, or mistakes in code that would cause a compilation error that prevents a successful build. +[**Errata** (39 items)](errata.md): Typos, tool user interface changes, or mistakes in code that would cause a compilation error that prevents a successful build. [**Improvements** (26 items)](improvements.md): Changes to text or code that would improve the content. These are optional. diff --git a/docs/errata/errata.md b/docs/errata/errata.md index 80632c0..126df6d 100644 --- a/docs/errata/errata.md +++ b/docs/errata/errata.md @@ -1,4 +1,4 @@ -**Errata** (38 items) +**Errata** (39 items) If you find any mistakes, then please [raise an issue in this repository](https://github.com/markjprice/cs11dotnet7/issues) or email me at markjprice (at) gmail.com. @@ -35,6 +35,9 @@ If you find any mistakes, then please [raise an issue in this repository](https: - [Page 399 - Managing directories](#page-399---managing-directories) - [Page 362 - Joining, formatting, and other string members](#page-362---joining-formatting-and-other-string-members) - [Page 412 - Compressing streams](#page-412---compressing-streams) +- [Page 454 - Scaffolding models using an existing database](#page-454---scaffolding-models-using-an-existing-database) + - [Category class changes](#category-class-changes) + - [NorthwindDb class changes](#northwinddb-class-changes) - [Page 477 - Inserting entities](#page-477---inserting-entities) - [Page 548 - Creating a class library for a Northwind database context](#page-548---creating-a-class-library-for-a-northwind-database-context) - [Page 551 - Creating a class library for entity models using SQL Server](#page-551---creating-a-class-library-for-entity-models-using-sql-server) @@ -465,6 +468,88 @@ It should be: WriteLine("The compressed contents:"); ``` +# Page 454 - Scaffolding models using an existing database + +> Thanks to [Masoud](https://github.com/MAS-OUD) for raising this [issue on 6 April 2023](https://github.com/markjprice/cs11dotnet7/issues/54). + +## Category class changes + +In Step 5, I show the class that represents a `Category` in the Northwind database that is generated by the `dotnet-ef`, as shown in the following code: +```cs +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +namespace WorkingWithEFCore.AutoGen +{ + [Index("CategoryName", Name = "CategoryName")] + public partial class Category + { + public Category() + { + Products = new HashSet(); + } + + [Key] + public int CategoryId { get; set; } + + [StringLength(15)] + public string CategoryName { get; set; } + + [Column(TypeName = "ntext")] + public string? Description { get; set; } + + [Column(TypeName = "image")] + public byte[]? Picture { get; set; } + + [InverseProperty("Category")] + public virtual ICollection Products { get; set; } + } +} +``` +The current `dotnet-ef` tool generates slightly different output, for example, it uses a file-scoped namespace declaration to avoid indenting and it initializes the `Products` property to a `List` instead of a `HashSet`, as shown in the following code: +```cs +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +namespace WorkingWithEFCore.AutoGen; + +[Index("CategoryName", Name = "CategoryName")] +public partial class Category +{ + [Key] + public int CategoryId { get; set; } + + [StringLength(15)] + public string CategoryName { get; set; } = null!; + + [Column(TypeName = "ntext")] + public string? Description { get; set; } + + [Column(TypeName = "image")] + public byte[]? Picture { get; set; } + + [InverseProperty("Category")] + public virtual ICollection Products { get; } = new List(); +} +``` + +## NorthwindDb class changes + +In Step 7, I show the class that represents the Northwind database that was generated by the `dotnet-ef` tool. The current `dotnet-ef` tool generates slightly different output, for example, it uses a file-scoped namespace declaration to avoid indenting and it does not set each `DbSet` property to the null-forgiving operator, as shown in the following code: +```cs +// older version of dotnet-ef did this: +public virtual DbSet Categories { get; set; } = null!; + +// current version of dotnet-ef does this: +public virtual DbSet Categories { get; set; } +``` + # Page 477 - Inserting entities > Thanks to [Chadwick Geyser](https://github.com/chadwickgeyser) for raising this [issue on 29 November 2022](https://github.com/markjprice/cs11dotnet7/issues/5).