azure - Azure Web App 中 EntityFramework Core 的连接字符串

标签 azure asp.net-core entity-framework-core asp.net-core-mvc azure-sql-database

我的 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 字符串,但出于开发目的,我希望在某个时候将其添加回来。

最佳答案

最新

代码优先。

Database Initialization in Entity Framework 6

connectionStr 应该是这样的。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
    <add name="SchoolDBConnectionString" connectionString="tcp:servername.database.windows.net,1433;Initial Catalog=db; Persist Security Info=False; User ID=user;Password=mypassword; MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

隐私

数据库优先。

您的项目使用 ef core,因此 connectionstrings 应如下所示。

<connectionStrings>
<add name="SchoolDBEntities" connectionString="metadata=res://*/SchoolDB.csdl|res://*/SchoolDB.ssdl|res://*/SchoolDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=SchoolDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>

更多详情,可以引用我在帖子中的回答。

Timeout period elasped prior to obtaining a connection from the pool - Entity Framework

您也可以引用本教程,它会对您有所帮助。

Entity Framework Tutorial

关于azure - Azure Web App 中 EntityFramework Core 的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64761592/

相关文章:

使用 Azure Batch 的 Azure ADF 引发共享访问签名生成错误

asp.net-core - 使用 EFcore/ASP.net core 更新查找表中的数据的正确方法?我是用种子方法还是其他方法来做到这一点?

Node.JS + SQL Server + MongoDB + 云上的 Redis

android - 自动检测语音识别语言

c# - ASP.NET Core 处理方法不允许 405

c# - asp.net CORE 迁移生成空

asp.net-core - 添加 XML 支持时无法编译 ASP.NET 5 Web API

c# - 在 ASP.NET 5 中使用自定义 UserStore 和 RoleStore

c# - 为什么第一次请求需要更多时间?

ssl - 共享模式下 Azure 网站的自定义域 SSL 证书