Skip to content

Commit 71482ca

Browse files
committed
Fix issues with excluded tables; update ReadMe.
1 parent 53a243d commit 71482ca

File tree

17 files changed

+173
-115
lines changed

17 files changed

+173
-115
lines changed

README.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Before creating a pull request, please refer to the [Contributing Guidelines](ht
1414

1515
## Prerequisites
1616

17-
- [Visual Studio 2019](https://www.visualstudio.com/downloads/) 16.8 Preview 4.0 or greater.
18-
- .[NET Core 5.0 SDK](https://dotnet.microsoft.com/download/dotnet/5.0) RC2 or greater.
19-
- [EF Core CLI 5.0](https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-ef-core-5-rc2/) or greater.
17+
- [Visual Studio 2022](https://www.visualstudio.com/downloads/) or greater.
18+
- [.NET Core 6.0 SDK](https://dotnet.microsoft.com/download/dotnet/6.0) RC2 or greater.
19+
- [EF Core CLI 6.0](https://docs.microsoft.com/en-us/ef/core/cli/dotnet) or greater.
2020
- Install global `dotnet-ef` tool.
2121
```
2222
dotnet tool install --global dotnet-ef
@@ -37,10 +37,7 @@ Before creating a pull request, please refer to the [Contributing Guidelines](ht
3737
3838
## Usage
3939
40-
1. Create a new **.NET Core** class library.
41-
- If necessary, edit the csproj file to update the **TargetFramework** to 3.1 or 5.0
42-
43-
> **Note**: Using the EF Core toolchain with a _.NET Standard_ class library is currently not supported. Instead, you can add a .NET Standard class library to the same solution as the .NET Core library, then add existing items and select **Add As Link** to include entity classes.
40+
1. Create a new **.NET 6** class library.
4441
4542
2. Add EF Core SQL Server and Tools NuGet packages.
4643
- `Microsoft.EntityFrameworkCore.SqlServer`
@@ -93,7 +90,7 @@ Before creating a pull request, please refer to the [Contributing Guidelines](ht
9390
9491
## Nullable Reference Types
9592
96-
Take advantage of C# nullable reference types by enabling them in your .csproj file.
93+
Take advantage of C# nullable reference types by enabling them in your .csproj file. (This is by default in .NET 6.)
9794
9895
```xml
9996
<PropertyGroup>
@@ -103,21 +100,12 @@ Take advantage of C# nullable reference types by enabling them in your .csproj f
103100
</PropertyGroup>
104101
```
105102

106-
Then enable nullable reference types for Handlebars scaffolding.
107-
108-
```csharp
109-
services.AddHandlebarsScaffolding(options =>
110-
{
111-
options.EnableNullableReferenceTypes = true;
112-
});
113-
```
114-
115103
Non-nullable properties will include the [null forgiving operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving).
116104

117105
```csharp
118106
public partial class Product
119107
{
120-
public string ProductName { get; set; } = default!;
108+
public string ProductName { get; set; } = null!;
121109
public decimal? UnitPrice { get; set; }
122110
}
123111
```
@@ -130,7 +118,7 @@ You can optionally exclude certain tables from code generation. These may also b
130118
services.AddHandlebarsScaffolding(options =>
131119
{
132120
// Exclude some tables
133-
options.ExcludedTables = new List<string> { "Territory", "dbo.EmployeeTerritories" };
121+
options.ExcludedTables = new List<string> { "dbo.Territory" };
134122
});
135123
```
136124

sample/ScaffoldingSample/CodeTemplates/CSharpDbContext/Partials/DbImports.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.EntityFrameworkCore;
34
using Microsoft.EntityFrameworkCore.Metadata; // Comment
45
{{#if model-namespace}}

sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Microsoft.EntityFrameworkCore;
34
using Microsoft.EntityFrameworkCore.Metadata; // Comment
45
using dbo = ScaffoldingSample.Models.dbo;
@@ -8,19 +9,21 @@ namespace ScaffoldingSample.Contexts
89
public partial class NorthwindSlimContext : DbContext
910
{
1011
// My Handlebars Helper
11-
public virtual DbSet<dbo.Category> Categories { get; set; } = default!;
12+
public virtual DbSet<dbo.Category> Categories { get; set; } = null!;
1213
// My Handlebars Helper
13-
public virtual DbSet<dbo.Customer> Customers { get; set; } = default!;
14+
public virtual DbSet<dbo.Customer> Customers { get; set; } = null!;
1415
// My Handlebars Helper
15-
public virtual DbSet<dbo.CustomerSetting> CustomerSettings { get; set; } = default!;
16+
public virtual DbSet<dbo.CustomerSetting> CustomerSettings { get; set; } = null!;
1617
// My Handlebars Helper
17-
public virtual DbSet<dbo.Employee> Employees { get; set; } = default!;
18+
public virtual DbSet<dbo.Employee> Employees { get; set; } = null!;
1819
// My Handlebars Helper
19-
public virtual DbSet<dbo.Order> Orders { get; set; } = default!;
20+
public virtual DbSet<dbo.Order> Orders { get; set; } = null!;
2021
// My Handlebars Helper
21-
public virtual DbSet<dbo.OrderDetail> OrderDetails { get; set; } = default!;
22+
public virtual DbSet<dbo.OrderDetail> OrderDetails { get; set; } = null!;
2223
// My Handlebars Helper
23-
public virtual DbSet<dbo.Product> Products { get; set; } = default!;
24+
public virtual DbSet<dbo.Product> Products { get; set; } = null!;
25+
// My Handlebars Helper
26+
public virtual DbSet<dbo.Territory> Territories { get; set; } = null!;
2427

2528
public NorthwindSlimContext(DbContextOptions<NorthwindSlimContext> options) : base(options)
2629
{
@@ -37,15 +40,11 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3740

3841
protected override void OnModelCreating(ModelBuilder modelBuilder)
3942
{
40-
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
41-
4243
modelBuilder.Entity<dbo.Category>(entity =>
4344
{
4445
entity.ToTable("Category");
4546

46-
entity.Property(e => e.CategoryName)
47-
.IsRequired()
48-
.HasMaxLength(15);
47+
entity.Property(e => e.CategoryName).HasMaxLength(15);
4948
});
5049

5150
modelBuilder.Entity<dbo.Customer>(entity =>
@@ -54,13 +53,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5453

5554
entity.Property(e => e.CustomerId)
5655
.HasMaxLength(5)
57-
.IsFixedLength(true);
56+
.IsFixedLength();
5857

5958
entity.Property(e => e.City).HasMaxLength(15);
6059

61-
entity.Property(e => e.CompanyName)
62-
.IsRequired()
63-
.HasMaxLength(40);
60+
entity.Property(e => e.CompanyName).HasMaxLength(40);
6461

6562
entity.Property(e => e.ContactName).HasMaxLength(30);
6663

@@ -76,11 +73,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
7673

7774
entity.Property(e => e.CustomerId)
7875
.HasMaxLength(5)
79-
.IsFixedLength(true);
76+
.IsFixedLength();
8077

81-
entity.Property(e => e.Setting)
82-
.IsRequired()
83-
.HasMaxLength(50);
78+
entity.Property(e => e.Setting).HasMaxLength(50);
8479

8580
entity.HasOne(d => d.Customer)
8681
.WithOne(p => p.CustomerSetting)
@@ -99,15 +94,26 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
9994

10095
entity.Property(e => e.Country).HasMaxLength(15);
10196

102-
entity.Property(e => e.FirstName)
103-
.IsRequired()
104-
.HasMaxLength(20);
97+
entity.Property(e => e.FirstName).HasMaxLength(20);
10598

10699
entity.Property(e => e.HireDate).HasColumnType("datetime");
107100

108-
entity.Property(e => e.LastName)
109-
.IsRequired()
110-
.HasMaxLength(20);
101+
entity.Property(e => e.LastName).HasMaxLength(20);
102+
103+
entity.HasMany(d => d.Territories)
104+
.WithMany(p => p.Employees)
105+
.UsingEntity<Dictionary<string, object>>(
106+
"EmployeeTerritory",
107+
l => l.HasOne<dbo.Territory>().WithMany().HasForeignKey("TerritoryId").HasConstraintName("FK_dbo.EmployeeTerritories_dbo.Territory_TerritoryId"),
108+
r => r.HasOne<dbo.Employee>().WithMany().HasForeignKey("EmployeeId").HasConstraintName("FK_dbo.EmployeeTerritories_dbo.Employee_EmployeeId"),
109+
j =>
110+
{
111+
j.HasKey("EmployeeId", "TerritoryId").HasName("PK_dbo.EmployeeTerritories");
112+
113+
j.ToTable("EmployeeTerritories");
114+
115+
j.IndexerProperty<string>("TerritoryId").HasMaxLength(20);
116+
});
111117
});
112118

113119
modelBuilder.Entity<dbo.Order>(entity =>
@@ -116,7 +122,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
116122

117123
entity.Property(e => e.CustomerId)
118124
.HasMaxLength(5)
119-
.IsFixedLength(true);
125+
.IsFixedLength();
120126

121127
entity.Property(e => e.Freight)
122128
.HasColumnType("money")
@@ -157,9 +163,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
157163
{
158164
entity.ToTable("Product");
159165

160-
entity.Property(e => e.ProductName)
161-
.IsRequired()
162-
.HasMaxLength(40);
166+
entity.Property(e => e.ProductName).HasMaxLength(40);
163167

164168
entity.Property(e => e.RowVersion)
165169
.IsRowVersion()
@@ -175,6 +179,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
175179
.HasConstraintName("FK_Products_Categories");
176180
});
177181

182+
modelBuilder.Entity<dbo.Territory>(entity =>
183+
{
184+
entity.ToTable("Territory");
185+
186+
entity.Property(e => e.TerritoryId).HasMaxLength(20);
187+
188+
entity.Property(e => e.TerritoryDescription).HasMaxLength(50);
189+
});
190+
178191
OnModelCreatingPartial(modelBuilder);
179192
}
180193

sample/ScaffoldingSample/Models/dbo/Category.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public Category()
1010
Products = new HashSet<Product>();
1111
}
1212

13-
public int CategoryId { get; set; } = default!;
14-
public string CategoryName { get; set; } = default!;
13+
public int CategoryId { get; set; }
14+
public string CategoryName { get; set; } = null!;
1515

16-
public virtual ICollection<Product> Products { get; set; } = default!;
16+
public virtual ICollection<Product> Products { get; set; }
1717

1818
// My Handlebars Block Helper: True
1919
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/Customer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public Customer()
1010
Orders = new HashSet<Order>();
1111
}
1212

13-
public string CustomerId { get; set; } = default!;
14-
public string CompanyName { get; set; } = default!;
13+
public string CustomerId { get; set; } = null!;
14+
public string CompanyName { get; set; } = null!;
1515
public string? ContactName { get; set; }
1616
public string? City { get; set; }
17-
public Country Country { get; set; } = default!;
17+
public Country? Country { get; set; } = null!;
1818

19-
public virtual CustomerSetting? CustomerSetting { get; set; }
20-
public virtual ICollection<Order> Orders { get; set; } = default!;
19+
public virtual CustomerSetting CustomerSetting { get; set; } = null!;
20+
public virtual ICollection<Order> Orders { get; set; }
2121

2222
// My Handlebars Block Helper: True
2323
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/CustomerSetting.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ScaffoldingSample.Models.dbo
55
{ // Comment
66
public partial class CustomerSetting : EntityBase // My Handlebars Helper
77
{
8-
public string CustomerId { get; set; } = default!;
9-
public string Setting { get; set; } = default!;
8+
public string CustomerId { get; set; } = null!;
9+
public string Setting { get; set; } = null!;
1010

11-
public virtual Customer Customer { get; set; } = default!;
11+
public virtual Customer Customer { get; set; } = null!;
1212

1313
// My Handlebars Block Helper: True
1414
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/Employee.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ namespace ScaffoldingSample.Models.dbo
55
{ // Comment
66
public partial class Employee : EntityBase // My Handlebars Helper
77
{
8-
public int EmployeeId { get; set; } = default!;
9-
public string LastName { get; set; } = default!;
10-
public string FirstName { get; set; } = default!;
8+
public Employee()
9+
{
10+
Territories = new HashSet<Territory>();
11+
}
12+
13+
public int EmployeeId { get; set; }
14+
public string LastName { get; set; } = null!;
15+
public string FirstName { get; set; } = null!;
1116
public DateTime? BirthDate { get; set; }
1217
public DateTime? HireDate { get; set; }
1318
public string? City { get; set; }
14-
public Country Country { get; set; } = default!;
19+
public Country? Country { get; set; } = null!;
20+
21+
public virtual ICollection<Territory> Territories { get; set; }
1522

1623
// My Handlebars Block Helper: True
1724
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/Order.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public Order()
1010
OrderDetails = new HashSet<OrderDetail>();
1111
}
1212

13-
public int OrderId { get; set; } = default!;
13+
public int OrderId { get; set; }
1414
public string? CustomerId { get; set; }
1515
public DateTime? OrderDate { get; set; }
1616
public DateTime? ShippedDate { get; set; }
1717
public int? ShipVia { get; set; }
1818
public decimal? Freight { get; set; }
1919

2020
public virtual Customer? Customer { get; set; }
21-
public virtual ICollection<OrderDetail> OrderDetails { get; set; } = default!;
21+
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
2222

2323
// My Handlebars Block Helper: True
2424
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/OrderDetail.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace ScaffoldingSample.Models.dbo
55
{ // Comment
66
public partial class OrderDetail : EntityBase // My Handlebars Helper
77
{
8-
public int OrderDetailId { get; set; } = default!;
9-
public int OrderId { get; set; } = default!;
10-
public int ProductId { get; set; } = default!;
11-
public decimal UnitPrice { get; set; } = default!;
12-
public short Quantity { get; set; } = default!;
13-
public float Discount { get; set; } = default!;
8+
public int OrderDetailId { get; set; }
9+
public int OrderId { get; set; }
10+
public int ProductId { get; set; }
11+
public decimal UnitPrice { get; set; }
12+
public short Quantity { get; set; }
13+
public float Discount { get; set; }
1414

15-
public virtual Order Order { get; set; } = default!;
16-
public virtual Product Product { get; set; } = default!;
15+
public virtual Order Order { get; set; } = null!;
16+
public virtual Product Product { get; set; } = null!;
1717

1818
// My Handlebars Block Helper: True
1919
// My Handlebars Block Helper: False

sample/ScaffoldingSample/Models/dbo/Product.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public Product()
1010
OrderDetails = new HashSet<OrderDetail>();
1111
}
1212

13-
public int ProductId { get; set; } = default!;
14-
public string ProductName { get; set; } = default!;
13+
public int ProductId { get; set; }
14+
public string ProductName { get; set; } = null!;
1515
public int? CategoryId { get; set; }
1616
public decimal? UnitPrice { get; set; }
17-
public bool Discontinued { get; set; } = default!;
17+
public bool Discontinued { get; set; }
1818
public byte[]? RowVersion { get; set; }
1919

2020
public virtual Category? Category { get; set; }
21-
public virtual ICollection<OrderDetail> OrderDetails { get; set; } = default!;
21+
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
2222

2323
// My Handlebars Block Helper: True
2424
// My Handlebars Block Helper: False

0 commit comments

Comments
 (0)