[dotnet-scaffold 20.0.0] Invalid --model when model and DbContext are in referenced projects
- Dominant language
- C#
- Stars
- 818
- Forks
- 260
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
## File a bug
`microsoft.dotnet-scaffold` 20.0.0 cannot discover an EF model or `DbContext` when they are declared in referenced class-library projects. The same solution scaffolds successfully with `dotnet-aspnet-codegenerator` 8.0.7.
### Reproduction
Create a .NET 8 solution containing:
- `Diagnosis.Web`: ASP.NET Core Razor Pages project.
- `Diagnosis.Entities`: class library containing a public `Product` entity.
- `Diagnosis.DataAccess`: class library containing `DiagnosisDbContext`.
References:
```text
Diagnosis.Web -> Diagnosis.Entities
Diagnosis.Web -> Diagnosis.DataAccess
Diagnosis.DataAccess -> Diagnosis.Entities
```
Register `DiagnosisDbContext` in the web application's dependency-injection container and verify that the whole solution builds successfully.
Run:
```powershell
dotnet scaffold aspnet razorpages-crud `
--project .\Diagnosis.Web\Diagnosis.Web.csproj `
--model Product `
--dataContext DiagnosisDbContext `
--dbProvider sqlserver-efcore `
--page CRUD
```
### Actual result
```text
Initializing scaffolding model...
Invalid --model 'Product'
An error occurred.
```
No files are generated. The process also returns exit code `0` despite reporting an error.
Using the fully qualified model name produces the same result.
### Expected result
The tool should discover public model and `DbContext` types in referenced projects and generate the Razor Pages CRUD files. It should also return a non-zero exit code when scaffolding fails.
### Control test
On an identical copy of the same solution, this command succeeds:
```powershell
dotnet aspnet-codegenerator razorpage `
-m Product `
-dc DiagnosisDbContext `
-udl `
-outDir Pages\Products `
--referenceScriptLibraries
```
Result:
```text
Added Razor Page : \Pages\Products\Create.cshtml
Added PageModel : \Pages\Products\Create.cshtml.cs
Added Razor Page : \Pages\Products\Edit.cshtml
Added PageModel : \Pages\Products\Edit.cshtml.cs
Added Razor Page : \Pages\Products\Details.cshtml
Added PageModel : \Pages\Products\Details.cshtml.cs
Added Razor Page : \Pages\Products\Delete.cshtml
Added PageModel : \Pages\Products\Delete.cshtml.cs
Added Razor Page : \Pages\Products\Index.cshtml
Added PageModel : \Pages\Products\Index.cshtml.cs
```
All 10 generated files compile successfully.
The test was repeated after pinning the solution to SDK `8.0.422` with `global.json`. `microsoft.dotnet-scaffold` 20.0.0 still produced the same `Invalid --model` error, so the selected .NET 10 SDK was not the cause.
### Suspected cause
In `ValidateRazorPagesStep.GetRazorPageModelAsync`, model and context lookup uses:
```csharp
var allClasses = await projectInfo.CodeService.GetAllClassSymbolsAsync();
var modelClassSymbol = allClasses.FirstOrDefault(
x => x.Name.Equals(settings.Model, StringComparison.OrdinalIgnoreCase));
```
`CodeService.GetAllClassSymbolsAsync()` obtains classes only from `_compilation.SyntaxTrees`. Referenced assemblies are metadata references and their types are not part of those syntax trees. Therefore public model and context types declared in referenced projects are omitted.
Relevant source:
- `src/dotnet-scaffolding/dotnet-scaffold/AspNet/ScaffoldSteps/ValidateRazorPagesStep.cs`
- `src/dotnet-scaffolding/Microsoft.DotNet.Scaffolding.Roslyn/Services/CodeService.cs`
### Provider and version information
```text
microsoft.dotnet-scaffold: 20.0.0
dotnet-aspnet-codegenerator control test: 8.0.7
dotnet-ef: 8.0.27
Target framework: net8.0
EF Core packages: 8.0.27
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.27
Operating system: Windows 10 x64, build 26200
Visual Studio: Community 2022 17.14.35 and Community 2026 18.7.1
.NET SDKs tested: 10.0.301 and pinned 8.0.422
```
### Additional observation
If a test model is moved directly into the web project, `microsoft.dotnet-scaffold` finds the model. However, it still fails to discover the existing `DbContext` from the referenced Data Access project and attempts to create a new context in the web project. This is consistent with referenced-project types being excluded from class discovery.
Contributor guide
Assessment
This issue has not been assessed yet.