【问题标题】:Can't add view using scaffolding . "There was an errror running the selected generator: Unable to resolve the service for type.."无法使用脚手架添加视图。 “运行选定的生成器时出错:无法解析类型的服务..”
【发布时间】:2022-01-24 08:59:23
【问题描述】:

我无法使用脚手架创建剃刀视图。我的项目是一个使用 net6.0 的 ASP.NET Core Web App MVC。我也用VS2022。

这是我得到的错误:Scaffolding Error

我尝试重新启动我的 vs 但没有成功。

这是我的 DbContext:

public class SnowballDbContext : DbContext
    {
        public SnowballDbContext(DbContextOptions<SnowballDbContext> options) : base(options)
        {

        }
        public DbSet<User> Users { get; set; }
        
        public virtual DbSet<Habit> Habits { get; set; }
        
        public virtual DbSet<Tracker> Trackers { get; set; }

        public virtual DbSet<Progression> Progressions { get; set; }

        public virtual DbSet<CalendarDay> CalendarDays { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Tracker>()
                .HasKey(t => t.HabitId);
           

        }

    }

这是我的程序.cs:

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DataBaseConnectionString");

builder.Services.AddDbContext<SnowballDbContext>(options => options.UseSqlServer(connectionString));


// Add services to the container.
builder.Services.AddControllersWithViews();

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();

这是我的控制器:

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly SnowballDbContext _context;

        public HomeController(ILogger<HomeController> logger, SnowballDbContext context)
        {
            _logger = logger;
            _context = context;
        }

        public IActionResult Index()
        {
            return View(_context.Habits.ToList());
        }

我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: c# asp.net-mvc scaffolding .net-6.0 visual-studio-2022


    【解决方案1】:

    检查你是否安装了这个 nuget 包

    Microsoft.VisualStudio.Web.CodeGeneration.Utils
    

    如果没有,请添加并重试

    【讨论】:

    • 我安装了这个包但是结果是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2015-04-07
    • 2017-10-15
    • 2016-05-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多