dotnet / Scaffolding

Code generators to speed up development.
MIT License
640 stars 228 forks source link

Scaffolding doesn't work if you don't have a class named Startup in your project. #956

Open BjarkeCK opened 5 years ago

BjarkeCK commented 5 years ago

Steps to reproduce:

  1. Create a new empty dotnet core project.
  2. Add a code-first DbContext with a simple table.
  3. (Optional) Run scaffold razor CRUD pages, from visual studio or from dotnet command line, and see that scaffolding works.
  4. Undo that, and now rename the Startup class to something else and then run scaffolding again.
  5. Now you should see the following error: "There was an error running the selected code generator: 'Object reference not set ot an instance of an object.' image
  6. Now create a new emtpy class and simply call it Startup which is enough to make scaffolding work again.

Target framework(s):

It happens in all versions from what I can tell. 2.1.x and 2.2.x but here is one combination:

 <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.2" />
  </ItemGroup>

Side note:

Please please please provide more details in the error message! I've spent so many hours on this one because it occurs for many different reasons it seems.

deepchoudhery commented 4 years ago

Currently, we do rely on the startup file being named "Startup.cs" for ASP.NET Core Web projects. Looking for a fix if the startup file's name is changed.