STEPS TO REPRODUCE - WITHOUT IDENTITY (.NET 6 & .NET 7)
- Create a new ASP.NET MVC Core Project
- Select Framework
- Leave Authentication set to default (none)
- Build project after it loads.
- Add packages for Entity Framework
- Add Data folder
- Create ApplicationDbContext Class in Data Folder
- Add Simple Model (Student>
- Add DbSet<Student> property to ApplicationDbContext
- Register in Project file
- Added Migration
- Updated Database
- Add => New Scaffolded Item => Controller
- Select Model and Data Context
The following error occurs.
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.