Add item for page 454

This commit is contained in:
Mark J Price 2023-04-06 14:58:00 +01:00
parent ea406c2372
commit 21a6245711
2 changed files with 87 additions and 2 deletions

View file

@ -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.

View file

@ -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<Product>();
}
[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<Product> 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<T>` instead of a `HashSet<T>`, 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<Product> Products { get; } = new List<Product>();
}
```
## 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<T>` property to the null-forgiving operator, as shown in the following code:
```cs
// older version of dotnet-ef did this:
public virtual DbSet<Category> Categories { get; set; } = null!;
// current version of dotnet-ef does this:
public virtual DbSet<Category> 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).