Sql server 将MVC核心应用程序连接到MS SQL Server的步骤是什么
我一直在尝试使用Visual Studio代码将Ms SQL server与MVC核心应用程序连接。 到目前为止,我补充说Sql server 将MVC核心应用程序连接到MS SQL Server的步骤是什么,sql-server,asp.net-core,visual-studio-code,asp.net-core-mvc,Sql Server,Asp.net Core,Visual Studio Code,Asp.net Core Mvc,我一直在尝试使用Visual Studio代码将Ms SQL server与MVC核心应用程序连接。 到目前为止,我补充说 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext<MvcMovieContext>(options => options.UseSqlServer(Configuration.GetCo
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<MvcMovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
在appsettings.json文件中我收到一个错误
值不能为null。参数名称:connectionString
在appsetting.json中,使用如下连接字符串-
{
"ConnectionStrings": {
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
},
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}
在startup.cs中,像这样使用它-
{
"ConnectionStrings": {
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
},
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“博客数据库”));
}
有关更多信息,请参阅官方文档