【发布时间】:2020-11-10 01:30:11
【问题描述】:
我的 Azure Web 应用程序中的 Azure SQL DB (SQL PaaS) 连接字符串存在问题。它在应用程序配置的“连接字符串”设置中定义。我在https://myAppName.scm.azurewebsites.net/Env.cshtml#connectionStrings 中看到了 Conn 字符串值(格式如下)。我定义的 Conn 字符串名称是“MyApp”,Azure 会按照您的预期为其添加前缀“SQLAZURECONNSTR_”。
SQLAZURECONNSTR_MyApp = Server=tcp:mysvr.database.windows.net,1433;Initial Catalog=MyApp;Persist Security Info=False;User ID=user@mysvr;Password=passWord;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
在 Startup.cs 中,我有这个,但我不确定是否有必要。
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddDbContext<MyAppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyApp")));
在我的数据库上下文类中,我有以下内容:
public partial class MyAppContext : DbContext
{
[...]
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.Build();
var connectionString = configuration.GetConnectionString("MyApp");
optionsBuilder.UseSqlServer(connectionString);
}
}
}
当访问执行连接到数据库的方法的 Web 应用程序时,我收到对 conn 字符串的空引用。
ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName)
Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action<SqlServerDbContextOptionsBuilder> sqlServerOptionsAction)
myapp.Models.MyAppContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in MyAppContext.cs
本地 .json 文件中未定义任何 conn 字符串,但我会想在某些时候将其添加回来以用于开发目的。
【问题讨论】:
-
您的问题解决了吗?有进展吗?
标签: azure asp.net-core entity-framework-core asp.net-core-mvc azure-sql-database