使用 Asp.net 核心生成带有视图的控制器
Generating a Controller with views Using Asp.net Core
我使用项目 asp.net 核心但是当我想为模型生成带有视图的控制器时出现错误:
There was an error creating the DbContext instance to get the model.
No parameterless constructor defined for this object. No parameterless
constructor defined for this object. at
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
at
Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[]
args) at
Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[]
args)
这是 DBContext :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using WebApplication.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using MySQL.Data.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
namespace WebApplication
{
public class HunterViewContext : DbContext
{
public HunterViewContext(DbContextOptions<HunterViewContext> options)
: base(options)
{ }
public DbSet<User> User{ get; set; }
}
}
DBContextFactory:
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication.Models
{
public static class HunterViewContextFactory
{
public static HunterViewContext Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<HunterViewContext> ();
optionsBuilder.UseMySQL(connectionString);
//Ensure database creation
var context = new HunterViewContext(optionsBuilder.Options);
context.Database.EnsureCreated();
return context;
}
}
}
模特:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
namespace WebApplication.Models
{
public class User
{
public User()
{
}
public int Id { get; set; }
[MaxLength(30)]
public string Name { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
}
}
这是我想为模型用户创建带有视图的 MVC 控制器时的错误
您是否正在尝试使用 Visual Studio 代码脚手架功能来自动生成控制器 class 和视图?如果是这样,ASP.Net 核心项目模板尚不支持它。
您需要自己创建视图和控制器classes,参考文档Add Controllers
打开 project.json 文件并添加以下包依赖项和工具
在依赖部分
添加以下包
//代码生成器包生成控制器,视图
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"
在工具部分
添加以下工具
//访问命令工具代码生成
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"
我使用项目 asp.net 核心但是当我想为模型生成带有视图的控制器时出现错误:
There was an error creating the DbContext instance to get the model. No parameterless constructor defined for this object. No parameterless constructor defined for this object. at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
这是 DBContext :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using WebApplication.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using MySQL.Data.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
namespace WebApplication
{
public class HunterViewContext : DbContext
{
public HunterViewContext(DbContextOptions<HunterViewContext> options)
: base(options)
{ }
public DbSet<User> User{ get; set; }
}
}
DBContextFactory:
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication.Models
{
public static class HunterViewContextFactory
{
public static HunterViewContext Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<HunterViewContext> ();
optionsBuilder.UseMySQL(connectionString);
//Ensure database creation
var context = new HunterViewContext(optionsBuilder.Options);
context.Database.EnsureCreated();
return context;
}
}
}
模特:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
namespace WebApplication.Models
{
public class User
{
public User()
{
}
public int Id { get; set; }
[MaxLength(30)]
public string Name { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
}
}
这是我想为模型用户创建带有视图的 MVC 控制器时的错误
您是否正在尝试使用 Visual Studio 代码脚手架功能来自动生成控制器 class 和视图?如果是这样,ASP.Net 核心项目模板尚不支持它。 您需要自己创建视图和控制器classes,参考文档Add Controllers
打开 project.json 文件并添加以下包依赖项和工具
在依赖部分
添加以下包
//代码生成器包生成控制器,视图
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"
在工具部分
添加以下工具
//访问命令工具代码生成
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"