【发布时间】:2018-11-03 10:36:34
【问题描述】:
我收到此错误:
InvalidOperationException:没有为此 DbContext 配置数据库提供程序。可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。如果使用了 AddDbContext,那么还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。
我尝试了一切,但又成功了。我想定义启动连接字符串,但我不能。
我的ApplicationDbContext 是:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(){}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options){}
public DbSet<Course> Courses { get; set; }
public DbSet<CourseType> CourseTypes { get; set; }
public DbSet<CourseState> CourseStates { get; set; }
public DbSet<Topic> Topics { get; set; }
public DbSet<Heding> Hedings { get; set; }
}
我的启动是:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DbLearning"))));
}
我的appSetting.json 是:
"ConnectionStrings": {
"DbLearning": "Server=(localdb)\\mssqllocaldb;Database=DbLearning;Trusted_Connection=True;"
}
问题是在启动中但在onConfiguring 没有问题
【问题讨论】:
-
你的 AddDbContext 中的 serviceProvider 是什么?
-
删除它并更改代码
-
你也有同样的错误?
-
是的,正是:(
标签: c# asp.net-mvc entity-framework-6 asp.net-core-2.1