控制器脚手架在VS 2017预览版中不适用于ASP.NET Core 2预览版 [英] Controller scaffolding doesn't work in VS 2017 preview for ASP.NET Core 2 Preview

本文介绍了控制器脚手架在VS 2017预览版中不适用于ASP.NET Core 2预览版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过实体框架使用脚手架生成带有视图的MVC控制器:

I'm trying to use scaffolding to generate MVC Controller with views, using Entity Framework:

我创建了ApplicationDBContext:

I created ApplicationDBContext:

public class ApplicationDbContext : DbContext
    {        

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }           

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

并添加到ConfigureServices:

and added to ConfigureServices:

services.AddDbContext<Data.ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

当然我有众议院模型:

public class House
{
    public int ID { get; set; }
    public string HouseName { get; set; }
    public string Address1 { get; set; }
    public int CityID { get; set; }
    public City City { get; set; }
    public string ZIP { get; set; }
    public int StateID { get; set; }
    public State State { get; set; }
}

我收到以下错误:

好的,我向ApplicationDBContext添加了无参数构造函数:

Okay, I added parameterless constructor to ApplicationDBContext:

public ApplicationDbContext() { }

并收到以下错误:.

实际上,我在以前的项目中遇到了同样的问题,并手动创建了Controller和View.

Actually I had the same problem with previous project and created Controller and View manually.

但是对于这个项目,我想使用脚手架.

But for this project I'd like to use scaffolding.

我做错了,还是VS 2017预览问题?

I'm doing something wrong or it's VS 2017 preview problem?

推荐答案

您可能缺少此部分-

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.

这篇关于控制器脚手架在VS 2017预览版中不适用于ASP.NET Core 2预览版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆