欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VS2017添加EF的MVC控制器報錯的解決方法

 更新時間:2017年04月18日 14:13:09   作者:Capricornus  
這篇文章主要為大家詳細介紹了VS2017添加EF的MVC控制器報錯的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

VS2017添加EF的MVC控制器報錯的解決方法,供大家參考,具體內容如下

1. 錯誤描述:no database provider has been configured fot this DbContext.

此類錯誤是上下文的注冊造成的.解決方式在DBContext中重寫OnConfiguring方法去注入數(shù)據庫連接.

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開發(fā)使用教程》,關于visual studio的安裝教程可以點擊《Visual Studio安裝使用手冊》進行學習,希望大家喜歡。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論