Azure Functions - 如何连接到本地 SQL Server Express 数据库?

Azure Functions - how to connect to local SQL Server Express database?

我正在使用 Azure 函数创建我的第一个应用程序并尝试连接到本地 SQL Server Express 数据库。如何提供连接字符串以便我可以成功连接到本地 SQL Server Express 数据库?

当我尝试在本地 运行 应用程序时,出现以下错误:

System.ArgumentException occurred
HResult=0x80070057
Message=Keyword not supported: 'server'.
Source=

StackTrace:

at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary2 parsetable, String connectionString, IList1 validKeywords)
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList1 validKeywords) at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection) at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config) at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName() at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName() at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

我的 local.settings.json 看起来像这样:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "xxx",
        "AzureWebJobsDashboard": "xxx"
    },

    "ConnectionStrings": {
        "RecConnectionString": {
            "ConnectionString": "Server=localhost\SQLEXPRESS01;Database=myDB;Connection Timeout=30;Integrated Security=SSPI;",
            "ProviderName": "System.Data.EntityClient"
        }
    },
    "frameworks": {
        "net46": {
            "dependencies": {
                "EntityFramework": "6.0.0",
                "Newtonsoft.Json": "10.0.3"
            }
        }
    }
}

如错误消息所暗示:消息=关键字不受支持:'server'.

错误是由于在您的连接字符串中使用了 'server' 关键字造成的。

您应该将关键字 'server' 替换为“数据源

Data Source=.\SQLEXPRESS01;Database=myDB;Connection Timeout=30;Integrated Security=SSPI;

或者,您也可以将提供商名称修改为 System.Data.SqlClient,而不是更新现有的连接字符串。

供您参考,此问题也类似于下面 link 处的上一个问题:

Keyword not supported: 'server'

希望对您有所帮助。