1

我一直在嘗試使用Visual Studio代碼連接Ms SQL服務器和MVC核心應用程序。 到目前爲止,我已經添加什麼是連接MVC核心應用程序到MS SQL服務器的步驟

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(); 
    services.AddDbContext<MvcMovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 
} 

在startup.cs文件

"ConnectionStrings": { 
     "MvcMovieContext": "(Server=localdb)\\mssqllocaldb;Database=MvcMovieContext-20613a4b-deb5-4145-b6cc-a5fd19afda13;Trusted_Connection=True;MultipleActiveResultSets=true" 
    }, 
    "Data": { 
     "DefaultConnection": { 
      "ConnectionString": "MvcMovieContext" 
     } 
    } 

在appsettings.json文件。

我得到一個錯誤

值不能爲null.Parameter名稱:ConnectionString的

回答

1

在appsetting.json,使用連接字符串像這個 -

{ 
    "ConnectionStrings": { 
    "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;" 
    }, 
} 

並在startup.cs中,像這樣使用它 -

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddDbContext<BloggingContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"))); 
} 

欲瞭解更多信息,請參閱官方文檔here

+0

需要幫助。你能否請我的問題upvote,因爲我被阻止提出更多的問題,因爲投票少,意見少。我只問了7個問題。他們都沒有被低估。我必須擺脫這個禁令。任何建議都會有很大的幫助 –

相關問題