Unable to Resolve Scaffolding Issue in ASP.NET Core MVC Application

Xenu 0 Reputation points
2023-01-25T03:05:53.0133333+00:00

I'm getting the following error when I try to scaffold the Login, Logout, and Register pages.

ScaffoldingError

I ran the Visual Studio Installer to and clicked on the ASP.NET web development workflow to ensure I have all of the required tools installed.

Version information is below.

VisualStdioVersion

The error message isn't very helpful. I searched online, but couldn't find an issue like mine.

Any help would be very much appreciated.

Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,618 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,510 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,200 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Xenu 0 Reputation points
    2023-01-27T12:46:01.0833333+00:00

    Thank you Tianyu. I'll go through the process one more time, and document the steps.

    This issue occurs when attempting to scaffold identity and with general scaffolding as well. The error in my original post was encountered when I was attempting to scaffold an MVC Controller with Views using Entity Framework. (This project was created without Identity.)

    One other thing.

    I thought my issue might have something to do with my current version of Visual Studio. I decided to try the preview (17.5.0), and try again. I received basically the same error.

    0 comments No comments

  2. Xenu 0 Reputation points
    2023-01-27T13:08:05.1733333+00:00

    STEPS TO REPRODUCE ERROR - WITH IDENTITY

    1. Create a new ASP.NET MVC Core Project
    2. Select .NET 7.0
    3. Select Identity
    4. Build
    5. Change DefaultConnection string to point to local database in appsettings.json
    6. Run Update-Database
    7. Right click on project and select Add => New Scafffolded Item
    8. Select overrides (Login, Logout, Register)

    The error occurs at this point.

    I tried repeating steps 7 and 8, starting from the Areas, Identity, and Pages folders respectively. I got the same error.

    My Project File

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UserSecretsId>aspnet-LeaveManagement.Web-691c846b-101e-491b-b804-6cc534a6a891</UserSecretsId>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
      </ItemGroup>
    
    </Project>
    

    appsettings.json

      "ConnectionStrings": {
        "DefaultConnection": "Server=DESKTOP-F66RQ22;Database=LeaveManagement;Trusted_Connection=True;Encrypt=False;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*"
    }
    
    

    Error Message

    ScaffoldingErrorIdentityVSPreview

    I receive the same error message regardless of the targeted framework (.NET 7 vs .NET 6)

    The project's folder structure is the default generated by Visual Studio.

    0 comments No comments

  3. Xenu 0 Reputation points
    2023-01-27T13:12:47.6633333+00:00

    INSTALLATION DETAILS

    VisualStudioInstallationDetails

    0 comments No comments

  4. TheCoder 91 Reputation points
    2023-01-27T13:28:55.2166667+00:00

    I spent several days with this issue: If you're doing code first, add this to your DbContext file: and then the scaffolding will work.

    
    
    

  5. Xenu 0 Reputation points
    2023-01-27T13:52:10.8733333+00:00

    STEPS TO REPRODUCE THE ISSUE (.NET 6 and .NET 7) - WITH IDENTITY

    1. Create new ASP.NET Core MVC Project
    2. Select Targeted Framework
    3. Check Identity / Individual Accounts
    4. Build project after it loads.
    5. Change DefaultConnection in appsettings.json to point to local database.
    6. Run Update-Database in Package Manager Console to add Identity tables to database.
    7. Right click on Project folder and select Add => New Scaffolded Item => Identity
    8. Select overrides (Login, Logout, Register)
    9. Select DbContext
    10. Scaffolding begins but doesn't complete successfully. The following error is displayed.

    ScaffoldingErrorIdentityVSPreview

    I tried repeating the scaffolding operation, right clicking on different folders (Areas, Identity, Pages) but I received the same error. (This seemed worth a try based on the reference to Path in the error message.)

    Project File

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UserSecretsId>aspnet-LeaveManagement.Web-691c846b-101e-491b-b804-6cc534a6a891</UserSecretsId>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
      </ItemGroup>
    
    </Project>
    
    

    appsettings.json

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=DESKTOP-F66RQ22;Database=LeaveManagement;Trusted_Connection=True;Encrypt=False;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*"
    }
    
    

    As I said before, the error results were the same regardless of the Visual Studio 2022 version (17.4.4 vs 17.5).

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.