【问题标题】:Connection String for EntityFramework Core in Azure Web AppAzure Web App 中 EntityFramework Core 的连接字符串
【发布时间】: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


【解决方案1】:

最新

代码优先。

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

【讨论】:

  • 该 conn 字符串仅对 DB 优先模型有效。我首先使用代码。我的帖子中的 conn 字符串原样工作,因为我在本地开发机器上运行它,前提是它位于 appsettings.json 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2013-03-09
  • 2018-05-28
  • 1970-01-01
  • 2015-09-14
  • 2011-08-13
相关资源
最近更新 更多