C# 尚未为此DbContext配置任何数据库提供程序。尝试更新数据库时出错
当我更新数据库时,出现以下错误: 尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数 我希望有人能帮我解决我的问题 这是我的上下文类和启动类C# 尚未为此DbContext配置任何数据库提供程序。尝试更新数据库时出错,c#,sql-server,asp.net-core-mvc,entity-framework-core,C#,Sql Server,Asp.net Core Mvc,Entity Framework Core,当我更新数据库时,出现以下错误: 尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数 我希望有人能帮我解决我的问题 这是我的上下文类和启动类 DataBaseContext.cs using Micr
DataBaseContext.cs
using Microsoft.EntityFrameworkCore;
using MovieExampleAppDotNetCore.Models;
namespace MovieExampleApp.Persistention
{
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> context) : base(context)
{
//Database.Initialize(true);
}
public DatabaseContext()
{
}
public DatabaseContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Movie> Movies { get; set; }
public DbSet<Customer> Customers { get; set; }
}
}
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.Formatters;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用Microsoft.Extensions.Options;
使用Microsoft.EntityFrameworkCore.SqlServer;
使用System.Linq;
使用MovieExampleApp.persistension;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc.Infrastructure;
命名空间MovieExampleAppDotNetCore
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(
options=>options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.AddControllersWithViews().AddNewtonsoftJson();
services.addcontrollerswithview(选项=>
{
Insert(0,GetJsonPatchInputFormatter());
});
}
私有静态NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
var builder=newservicecolection()
.AddLogging()
.AddMvc()
.AddNewtonsoftJson()
.Services.BuildServiceProvider();
返回生成器
.GetRequiredService()
价值
.输入格式化程序
第()类
.First();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
}
}
}
您检查过这个吗?DefaultConnectionIt是我的connectionstring。为什么在DbContext中需要3个构造函数?具有通用DbContextOptions的选项就足够了。尝试删除另外两个当我删除另外两个时,它会给我一个错误。什么错误?连接字符串是什么样子的?错误抱怨连接字符串中缺少信息
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore.SqlServer;
using System.Linq;
using MovieExampleApp.Persistention;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace MovieExampleAppDotNetCore
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<DatabaseContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddControllersWithViews(options =>
{
options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
});
}
private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
{
var builder = new ServiceCollection()
.AddLogging()
.AddMvc()
.AddNewtonsoftJson()
.Services.BuildServiceProvider();
return builder
.GetRequiredService<IOptions<MvcOptions>>()
.Value
.InputFormatters
.OfType<NewtonsoftJsonPatchInputFormatter>()
.First();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}