You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The bug occurs when trying to run dotnet aspnet-codegenerator and specify a DbContext:
dotnet aspnet-codegenerator controller -name ArtistsController -m Artist -dc MyDbContext
If the application's Program.Main method calls any extension methods, the tool fails with:
Could not get the reflection type for DbContext : MyDbContext
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
This happens with projects that reference Microsoft.VisualStudio.Web.CodeGeneration.Design v8.0.0. The same code works fine with Microsoft.VisualStudio.Web.CodeGeneration.Design v7.0.1, running under both .NET 7 and under .NET 8.
Verified on .NET 8.0.101 Windows 10 Pro 10.0.19045
Verified on .NET 8.0.101 on Linux (Ubuntu 20.04 on Windows WSL2)
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
/* The codegen command that reproduces the problem:
dotnet aspnet-codegenerator controller -name ArtistsController -m Artist -dc MyDbContext
If you uncomment the call to builder.DoNothing(), this command fails with:
Could not get the reflection type for DbContext : MyDbContext
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
*/
// builder.DoNothing();
builder.Services.AddControllersWithViews();
var sqliteConnection = new SqliteConnection("Data Source=:memory:");
sqliteConnection.Open();
builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlite(sqliteConnection));
var app = builder.Build();
using (var scope = app.Services.CreateScope()) {
var db = scope.ServiceProvider.GetService<MyDbContext>()!;
db.Database.EnsureCreated();
}
app.UseStaticFiles();
app.UseRouting();
app.MapGet("/", (MyDbContext db) => db.Artists.ToList());
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
app.Run();
public static class ExtensionMethods {
public static bool DoNothing(this WebApplicationBuilder builder) => true;
}
public class Artist {
public Guid Id { get; set; }
public string Name { get; set; } = String.Empty;
}
public class MyDbContext : DbContext {
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public DbSet<Artist> Artists { get; set; } = default!;
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Artist>().HasData(
new Artist { Id = Guid.NewGuid(), Name = "Artist A" },
new Artist { Id = Guid.NewGuid(), Name = "Artist B" },
new Artist { Id = Guid.NewGuid(), Name = "Artist C" }
);
}
}
The text was updated successfully, but these errors were encountered:
The bug occurs when trying to run
dotnet aspnet-codegenerator
and specify aDbContext
:If the application's
Program.Main
method calls any extension methods, the tool fails with:This happens with projects that reference
Microsoft.VisualStudio.Web.CodeGeneration.Design
v8.0.0. The same code works fine withMicrosoft.VisualStudio.Web.CodeGeneration.Design
v7.0.1, running under both .NET 7 and under .NET 8.Code is below, and there are .NET 7 and .NET 8 projects demonstrating the problem available at github.com/dylanbeattie/dotnet8-aspnet-codegenerator-bug.
The text was updated successfully, but these errors were encountered: