如何彻底解决在VS2017中添加EF MVC控制器时出现的错误问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计410个文字,预计阅读时间需要2分钟。
在VS2017中添加EF的MVC控制器报错解决方法,参考如下:
1. 错误描述: - No database provider has been configured for this DbContext. This error occurred while setting up the model. This error can be caused by an invalid connection string or an unconfigured database provider. - 此错误通常是由于DbContext的注册配置不正确导致的。
2. 解决方法: - 在DBContext中重新配置数据库连接字符串和数据库提供者。
示例代码: csharp public class MyDbContext : DbContext { public MyDbContext() { // 指定数据库提供者和连接字符串 Database.DefaultConnectionFactory=new SqlConnectionFactory(YourConnectionString); }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 配置模型 modelBuilder.Entity().ToTable(MyTable); // 其他配置... } }
VS2017添加EF的MVC控制器报错的解决方法,供大家参考,具体内容如下
1. 错误描述:no database provider has been configured fot this DbContext.
此类错误是上下文的注册造成的.解决方式在DBContext中重写OnConfiguring方法去注入数据库连接.
DbContext中:
public static string ConnectionString { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConnectionString); base.OnConfiguring(optionsBuilder); }
在Startup.cs中
public void ConfigureServices(IServiceCollection services) { // Add framework services. var sqlserverConnection = Configuration.GetConnectionString("SQLServerConnection"); DbContext.ConnectionString = sqlserverConnection;//将配置连接传入DbContext中 services.AddDbContext<DbContext>(options => options.UseSqlServer(sqlserverConnection)); services.AddMvc(); }
2.错误描述:Could not add Model type XXX to DbContext
错误描述没有注册DbSet属性.但实际上是有 public DbSet<XXX> XXX{ get; set; }注册的.将DbSet<XXX>中的类改成<命名空间+类名>这种完整声明即可解决
更多精彩内容大家可以点击《Visual Studio 2017开发使用教程》,关于visual studio的安装教程可以点击《Visual Studio安装使用手册》进行学习,希望大家喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计410个文字,预计阅读时间需要2分钟。
在VS2017中添加EF的MVC控制器报错解决方法,参考如下:
1. 错误描述: - No database provider has been configured for this DbContext. This error occurred while setting up the model. This error can be caused by an invalid connection string or an unconfigured database provider. - 此错误通常是由于DbContext的注册配置不正确导致的。
2. 解决方法: - 在DBContext中重新配置数据库连接字符串和数据库提供者。
示例代码: csharp public class MyDbContext : DbContext { public MyDbContext() { // 指定数据库提供者和连接字符串 Database.DefaultConnectionFactory=new SqlConnectionFactory(YourConnectionString); }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 配置模型 modelBuilder.Entity().ToTable(MyTable); // 其他配置... } }
VS2017添加EF的MVC控制器报错的解决方法,供大家参考,具体内容如下
1. 错误描述:no database provider has been configured fot this DbContext.
此类错误是上下文的注册造成的.解决方式在DBContext中重写OnConfiguring方法去注入数据库连接.
DbContext中:
public static string ConnectionString { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConnectionString); base.OnConfiguring(optionsBuilder); }
在Startup.cs中
public void ConfigureServices(IServiceCollection services) { // Add framework services. var sqlserverConnection = Configuration.GetConnectionString("SQLServerConnection"); DbContext.ConnectionString = sqlserverConnection;//将配置连接传入DbContext中 services.AddDbContext<DbContext>(options => options.UseSqlServer(sqlserverConnection)); services.AddMvc(); }
2.错误描述:Could not add Model type XXX to DbContext
错误描述没有注册DbSet属性.但实际上是有 public DbSet<XXX> XXX{ get; set; }注册的.将DbSet<XXX>中的类改成<命名空间+类名>这种完整声明即可解决
更多精彩内容大家可以点击《Visual Studio 2017开发使用教程》,关于visual studio的安装教程可以点击《Visual Studio安装使用手册》进行学习,希望大家喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

