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,201 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Xenu 0 Reputation points
    2023-01-27T14:42:57.0066667+00:00

    STEPS TO REPRODUCE - WITHOUT IDENTITY (.NET 6 & .NET 7)

    1. Create a new ASP.NET MVC Core Project
    2. Select Framework
    3. Leave Authentication set to default (none)
    4. Build project after it loads.
    5. Add packages for Entity Framework
    6. Add Data folder
    7. Create ApplicationDbContext Class in Data Folder
    8. Add Simple Model (Student>
    9. Add DbSet<Student> property to ApplicationDbContext
    10. Register in Project file
    11. Added Migration
    12. Updated Database
    13. Add => New Scaffolded Item => Controller
    14. Select Model and Data Context

    The following error occurs.

    ScaffoldingErrorVSPreviewStandard

    As you can see, it's the same error I encountered with a project that included Identity.

    Project File

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
      </ItemGroup>
    
    </Project>
    
    

    I also repeated steps 10 and 11 by right clicking on the Controllers folder: same error.

    appsettings.json

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

    Program.cs

    using Microsoft.EntityFrameworkCore;
    using WebApplication1.Data;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddControllersWithViews();
    builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
            builder.Configuration.GetConnectionString("DefaultConnection")
        ));
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    
    app.Run();
    

    ApplicationDbContext.cs

    using Microsoft.EntityFrameworkCore;
    using WebApplication1.Models;
    
    namespace WebApplication1.Data
    {
        public class ApplicationDbContext : DbContext
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
            {
    
            }
    
            public DbSet<Student> Students { get; set; }
        }
    }
    
    

    Student.cs

    namespace WebApplication1.Models
    {
        public class Student
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
            public string Phone { get; set; }
        }
    }
    

    This test project was as simple as I could make it, with a single simple model.

    I've provided the information that I thought might be helpful to you. Please let me know if you need anything else.

    Thanks again for your help with this (extremely frustrating) issue. It's very much appreciated.


  2. Xenu 0 Reputation points
    2023-01-27T20:36:13.24+00:00

    NEW ATTEMPT

    1. Install Windows Update 2023-01 Cumulative Update Preview for .NET Framework 3.5, 4.8, and 4.8.1 for Windows 10 Version 22H2 for x64 (KB5022478)
    2. Restarted PC
    3. Uninstalled VS 2022
    4. Reinstalled VS 2022
    5. Restarted PC
    6. Created new, simple ASP.NET Core MVC Project (no Identity) same as above, following the same steps.
    7. Got virtually the same error.

    NewScaffoldingErrorVSPreviewStandard


  3. Siamak Shirzad 20 Reputation points
    2024-05-22T21:07:41.5333333+00:00

    I was trying to add Identity as scaffolding in a .NET Core 8 (MVC) and I was getting the same error as Xenu. After couple of days struggling with the same issue, Finally I was able to resolve it. My applicationdbcontext class is in a different project (assembly), a data access layer with repository. Apparently, I was trying to add Identity to the Web layer (MVC) which is perfectly fine but getting that error.

    So what I did:

    I added some packages in my Web layer (screenshot below), not sure if it requires all of them or only Microsoft.AspNetCore.Identity.UI is enogh. My Data layer project (with ApplicationDbContext Class) is residing in a different project (assembly) as I mentioned earlier and it already has all required packages for entity framework core.

    User's image

    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.