[英]Why doesn't scaffolding work as expected?
I am trying to scaffold and I get the following error:我正在尝试搭建脚手架,但出现以下错误:
There was an error running the selected code generator: 'No parameterless constructor defined for type 'MvcProduct.Data.MvcProductContext'.'运行所选代码生成器时出错:“没有为类型‘MvcProduct.Data.MvcProductContext’定义无参数构造函数。”
Here you can see an image of it:在这里你可以看到它的图像:
The following is my MvcProductContext
:以下是我的MvcProductContext
:
using Microsoft.EntityFrameworkCore;
using MvcProduct.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcProduct.Data
{
public class MvcProductContext : DbContext
{
public MvcProductContext(DbContextOptions<MvcProductContext> options)
: base(options)
{
}
public DbSet<Product> Product { get; set; }
}
And the appsettings.json
:和appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MvcProductContext": "Server=(localdb)\\mssqllocaldb;Database=MvcProductContext-1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
ConfigureServices
method in Startup.cs
file: Startup.cs
文件中的ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<MvcProductContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MvcProductContext")));
}
I have also tried to add aa second constructor in MvcProductContext
class. (Something which I would like to avoid and don't want to do) A second contructor without any parameter.我还尝试在MvcProductContext
class 中添加第二个构造函数。(我想避免和不想做的事情)没有任何参数的第二个构造函数。 But if I do that I just get another error which says:但是,如果我这样做,我只会收到另一个错误消息:
There was an error running the selected code generator: 'No database provider has been configured for this DbContext.运行所选代码生成器时出错:'没有为此 DbContext 配置数据库提供程序。 A provider can be configured bu overriding the
DbContext.OnConfiguring
method or by usingAddDbContext
on the application service provider.可以通过覆盖DbContext.OnConfiguring
方法或在应用程序服务提供者上使用AddDbContext
来配置提供者。 IfAddDbContext
on the application service provider.如应用服务提供者上的AddDbContext
。 IfAddDbContext
is used, then also ensure that yourDbCotnext
type accepts aDbContextOptions<TContext>
object in its constructor and passes it to the base constructor forDbContext
.如果使用AddDbContext
,则还要确保您的DbCotnext
类型在其构造函数中接受DbContextOptions<TContext>
object 并将其传递给DbContext
的基本构造函数。
Microsoft is does the same.微软也是这样做的。 They are scaffolding an MVC controller with views, using Entity Framework.他们正在使用 Entity Framework 搭建带有视图的 MVC controller。 They are doing it without adding a second constructor in their MvcMovieCOntext class. Their MvcMovieContextClass corresponds to my MvcProductContext class.他们这样做时没有在他们的MvcMovieCOntext class 中添加第二个构造函数。他们的MvcMovieContextClass对应于我的MvcProductContext class。
Any help would be appreciated.任何帮助,将不胜感激。
Just add a IDesignTimeDbContextFactory implementation to your project and try scaffolding again.只需将IDesignTimeDbContextFactory实现添加到您的项目并再次尝试脚手架。 It will take care of instantiating your DbContext.它将负责实例化您的 DbContext。
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MvcProductContext>
{
public MvcProductContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json")
.Build();
var builder = new DbContextOptionsBuilder<MvcProductContext>();
var connectionString = configuration.GetConnectionString("MvcProductContext");
builder.UseSqlServer(connectionString);
return new MvcProductContext(builder.Options);
}
}
In .net core 6 :在.net 核心 6 中:
1.Microsoft.EntityFrameworkCore.SqlServer 1.微软.EntityFrameworkCore.SqlServer
2.Microsoft.EntityFrameworkCore.Tools 2.Microsoft.EntityFrameworkCore.工具
3.Microsoft.EntityFrameworkCore.Design 3.Microsoft.EntityFrameworkCore.设计
4.Microsoft.VisualStudio.Web.CodeGeneration.Design 4.Microsoft.VisualStudio.Web.CodeGeneration.Design
Scaffold the data base and create the DbContext and Entities by scaffold command or ef core power tools vs extension.通过 scaffold 命令或 ef core power tools vs extension 搭建数据库并创建 DbContext 和 Entities。
Inject your data base settings in program.cs:在 program.cs 中注入您的数据库设置:
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddDbContext<AppDbContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("CS")); });
Add your connection string to appsettings.json:将您的连接字符串添加到 appsettings.json:
"AllowedHosts": "*", "允许的主机": "*",
"ConnectionStrings": { "CS": "Data Source=.\\SQLEXPRESS;Initial Catalog=Db1;Integrated Security=True;MultipleActiveResultSets=True" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.