十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章給大家分享的是有關(guān)VS2017添加EF的MVC控制器報錯怎么辦的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

具體內(nèi)容如下
1. 錯誤描述:no database provider has been configured fot this DbContext.
此類錯誤是上下文的注冊造成的.解決方式在DBContext中重寫OnConfiguring方法去注入數(shù)據(jù)庫連接.
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(options => options.UseSqlServer(sqlserverConnection));
services.AddMvc();
} 2.錯誤描述:Could not add Model type XXX to DbContext
錯誤描述沒有注冊DbSet屬性.但實際上是有 public DbSet
感謝各位的閱讀!關(guān)于“VS2017添加EF的MVC控制器報錯怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!