可以检查以下几点以解决此问题:
"ConnectionStrings": {
"MyDatabase": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
}
try
{
var result = _context.MyTable.ToList();
}
catch (Exception ex)
{
// Handle exception
}
通过检查这些方面,应该可以避免ASP.NET Core MVC在引用数据库连接时生成异常错误。