且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制器脚手架在VS 2017预览版中不适用于ASP.NET Core 2预览版

更新时间:2023-08-28 22:42:10

您可能缺少此部分-

public class AppDbContextFactory : IDbContextFactory<AppDbContext>
{
    public AppDbContext Create(string[] args) =>
        Program.BuildWebHost(args).Services.GetRequiredService<AppDbContext>();
}

一旦在ASP.NET Core 2.0项目中启用了脚手架并将模型名称创建为房屋",请执行以下步骤-

Once you enable scaffolding in your ASP.NET Core 2.0 project and created model name as 'House', follow these steps-

第一步:在解决方案资源管理器中的Controllers文件夹上单击鼠标右键,然后选择Add> New Scaffolded Item.

Step1: Right click on the Controllers folder in Solution Explorer and select Add > New Scaffolded Item.

第2步::选择使用Entity Framework的带有视图的MVC控制器",然后单击添加".

Step2: Select 'MVC Controller with View, using Entity Framework' and click on Add.

步骤3::在添加控制器"中,选择房屋模型,然后使用加号(+)创建新的AppDbContext.还将添加上面提到的AppDbContextFactory.

Step3: In Add Controller, select House Model and use plus(+) sign to create new AppDbContext. It will also add above mentioned AppDbContextFactory.

完成步骤3后,您将在Controllers文件夹中具有HousesController,并且在views文件夹中具有相应的Houses文件夹.

After finishing step3, you will have HousesController in Controllers folder and corresponding Houses folder inside views folder.

这是我的AppDbContext外观-

This is my AppDbContext looks like-

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

    public DbSet<AspNetCore200_VS2017preview1_scaffolding.Models.House> House { get; set; }
}

希望这有助于进一步进行.

Hope this helps to proceed further.