最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
VS2017添加EF的MVC控制器报错解决方法
时间:2022-06-25 05:32:59 编辑:袖梨 来源:一聚教程网
VS2017添加EF的MVC控制器报错的解决方法,供大家参考,具体内容如下
1. 错误描述:no database provider has been configured fot this DbContext.
此类错误是上下文的注册造成的.解决方式在DBContext中重写OnConfiguring方法去注入数据库连接.
DbContext中:
publicstaticstringConnectionString {get;set; }
protectedoverridevoidOnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionString);
base.OnConfiguring(optionsBuilder);
}
在Startup.cs中
publicvoidConfigureServices(IServiceCollection services)
{
// Add framework services.
var sqlserverConnection = Configuration.GetConnectionString("SQLServerConnection");
DbContext.ConnectionString = sqlserverConnection;//将配置连接传入DbContext中
services.AddDbContext
services.AddMvc();
}
2.错误描述:Could not add Model type XXX to DbContext
错误描述没有注册DbSet属性.但实际上是有 public DbSet
相关文章
- 洛克王国曼陀罗战神介绍一览 04-19
- 支付宝蚂蚁森林神奇海洋4月18日答案 04-19
- 做面包时加盐的作用 制作面包时蚂蚁庄园4月18日答案 04-19
- 鱼睡觉会被水冲走吗 河里的鱼睡觉时蚂蚁庄园4.18答案 04-19
- 蚂蚁庄园今日答案最新2024.4.20 04-19
- 小鸡庄园今天答案2024.4.20 04-19