dotnet / dotnet/Scaffolding

NPE in Razor template List called without DB

Open
#1,773 0 comments 0 reactions 0 assignees View on GitHub
area-scaffolding customer-reported
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

## File a bug

When there is no database context defined and the selected template is **List**, the Razor generator fails with a null pointer exception in unknown code.

### Include your code

```C#
Dictionary
propertyLookup
= ((IModelMetadata)Model.ModelMetadata).Properties.ToDictionary(x => x.PropertyName, x => x);
```

### Include stack traces

Note: source heavily modified, line numbers do not match. It took me 3w to pull this thing apart and put it back together in a somewhat useful form (including to get any stack trace at all).

```
fail: Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand[813862114]
Code generation failed!
System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.)
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Razor.Template.ExecuteAsync()
at Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase.ExecuteTemplate() in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Templating/RazorTemplateBase.cs:line 36
at Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplating.RunTemplateAsync(String content, Object templateModel) in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Templating/RazorTemplating.cs:line 158
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorActionsService.AddFileFromTemplateAsync(String outputPath, String templateName, IEnumerable`1 templateFolders, Object templateModel) in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Core/CodeGeneratorActionsService.cs:line 118
at Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Razor.RazorPageScaffolderBase.GenerateView(RazorPageGeneratorModel razorGeneratorModel, ModelTypeAndContextModel modelTypeAndContextModel, String outputPath) in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageScaffolderBase.cs:line 269
at Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Razor.EmptyRazorPageScaffolder.GenerateCode(RazorPageGeneratorModel razorGeneratorModel) in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/EmptyRazorPageScaffolder.cs:line 58
at Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Razor.RazorPageGenerator.GenerateCode(RazorPageGeneratorModel razorPageGeneratorModel) in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageGenerator.cs:line 74
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__8_0() in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Core/ActionInvoker.cs:line 105
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) in /home/dell/.nuget/packages/microsoft.extensions.commandlineutils.sources/6.0.0-preview.3.21166.3/contentFiles/cs/netstandard1.0/CommandLine/CommandLineApplication.cs:line 374
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute() in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG.Core/ActionInvoker.cs:line 54
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute() in /home/dell/prog/Scaffolding/src/Scaffolding/VS.Web.CG/CodeGenCommand.cs:line 93
```

### Include provider and version information


Microsoft.VisualStudio.Web.CodeGeneration.Design/Microsoft.DotNet.MSIdentity version:
1.0.0

Target framework: (e.g. .NET 5.0)
6.0.1

Operating system:
Linux x64 5.15.8-1-default

IDE: (e.g. Visual Studio 2019 16.3)
1.63.2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.