azuresql-db-functions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseServerless API + event-driven on the Azure SQL Database container with Azure Functions
Azure SQL Database 容器上的无服务器 API + 事件驱动方案(Azure Functions)
Build HTTP CRUD endpoints and change-driven handlers over the local Azure SQL
Database container (Private Preview) using Azure Functions and the first-party
Azure SQL bindings. Two capabilities:
- API: HTTP-triggered functions with SQL input and output bindings (read and upsert with no ADO.NET boilerplate).
- Event-driven (local): the SQL trigger binding fires a function when rows are inserted/updated/deleted. It is backed by Change Tracking, runs fully locally against the container, and needs no cloud services.
Event-driven note: Azure SQL Change Event Streaming (CES) is the cloud path for streaming row changes, and it cannot run against the local container (it is unsupported on the Linux engine and streams only to Azure Event Hubs public endpoints). Locally, use the SQL trigger below. Open references/event-driven.md when you need the reason CES cannot run here, or when a user asks for it by name.
The container supports what the bindings need: Change Tracking on a user database,
compatibility level 130 or higher, and .
OPENJSON使用 Azure Functions 和第一方 Azure SQL bindings,在本地 Azure SQL Database 容器(私有预览版)上构建 HTTP CRUD 端点和变更驱动处理器。提供两项能力:
- API: 使用 SQL input 和 output bindings 的 HTTP 触发函数(无需 ADO.NET 样板代码即可读取和 upsert)。
- 事件驱动(本地): SQL trigger binding 会在插入、更新或删除行时触发函数。它基于 Change Tracking,完全在容器本地运行,且不需要云服务。
事件驱动说明:Azure SQL Change Event Streaming (CES) 是用于流式传输行变更的云端路径,无法在本地容器中运行(Linux 引擎不支持该功能,而且它仅可流向 Azure Event Hubs 的公共终结点)。在本地,请使用下文的 SQL trigger。当你需要了解 CES 无法在此运行的原因,或用户按名称询问 CES 时,请打开 references/event-driven.md。
容器支持 bindings 所需的功能:用户数据库上启用 Change Tracking、兼容级别为 130 或更高,以及 。
OPENJSONLoad-bearing facts (inlined; full engine detail in azuresql-db-container)
关键事实(已内联;完整的引擎详情见 azuresql-db-container)
- This is the Azure SQL Database engine (Private Preview), not the SQL Server
image .
mcr.microsoft.com/mssql/serverreturnsSERVERPROPERTY('EngineEdition'),5returnsSERVERPROPERTY('Edition').'SQL Azure' - Image: (x64,
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest). Registry is private: sign in first withlinux/amd64using the shared pull-only credentials from https://aka.ms/sqldbcontainerpreview-signup (they may rotate). Registry and tag are provisional during Private Preview.docker login sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io - Required env: and a complex
ACCEPT_EULA=Y(8+ chars, upper/lower/digit/symbol). Engine listens on 1433.MSSQL_SA_PASSWORD - The engine does NOT auto-create databases. Run on a master connection before the function app connects with
CREATE DATABASE appdb. Do not useDatabase=appdbto switch databases; select it in the connection string.USE - On a non-x64 host add .
--platform linux/amd64
For the full engine model (readiness loop, vectors, troubleshooting) see the
azuresql-db-container skill; to start the container and provision ,
use azuresql-db-container or azuresql-db-scaffold.
appdb- 这是 Azure SQL Database 引擎(私有预览版),而不是 SQL Server 镜像 。
mcr.microsoft.com/mssql/server返回SERVERPROPERTY('EngineEdition'),5返回SERVERPROPERTY('Edition')。'SQL Azure' - 镜像:(x64,
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest)。注册表为私有:先使用 https://aka.ms/sqldbcontainerpreview-signup 提供的共享仅拉取凭据执行linux/amd64(凭据可能会轮换)。私有预览期间,注册表和标签可能会变更。docker login sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io - 必需环境变量:和复杂的
ACCEPT_EULA=Y(至少 8 个字符,包含大写字母、小写字母、数字和符号)。引擎监听 1433 端口。MSSQL_SA_PASSWORD - 引擎不会自动创建数据库。请先在 master 连接上运行 ,然后函数应用才能使用
CREATE DATABASE appdb进行连接。不要使用Database=appdb切换数据库;应在连接字符串中选择该数据库。USE - 在非 x64 主机上,请添加 。
--platform linux/amd64
有关完整的引擎模型(就绪循环、向量、故障排查),请参阅 azuresql-db-container skill;要启动容器并预配 ,请使用 azuresql-db-container 或 azuresql-db-scaffold。
appdbStep 1: the connection string setting
第 1 步:连接字符串设置
The bindings read the connection string from an app setting. Use the name
(the docs' convention). In :
SqlConnectionStringlocal.settings.jsonjson
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"SqlConnectionString": "Server=localhost,1433;Database=appdb;User Id=sa;Password=<sa-password>;TrustServerCertificate=true"
}
}Replace with the container's ; stays out of
source control.
<sa-password>MSSQL_SA_PASSWORDlocal.settings.jsonTrustServerCertificate=trueFUNCTIONS_WORKER_RUNTIMEdotnet-isolatednodepythonpowershelljavaConnectionStringSettingconnectionStringSettingconnection_string_settingConnectionBindings 从应用设置中读取连接字符串。使用名称 (文档约定)。在 中:
SqlConnectionStringlocal.settings.jsonjson
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"SqlConnectionString": "Server=localhost,1433;Database=appdb;User Id=sa;Password=<sa-password>;TrustServerCertificate=true"
}
}将 替换为容器的 ; 不应纳入源代码管理。
<sa-password>MSSQL_SA_PASSWORDlocal.settings.json容器使用自签名证书,因此必须设置 。将 设置为你的语言(、、、、)。Bindings 通过 (C#/Java)、(function.json)或 (Python v2 decorator)引用此设置名称,而不是 (该关键字用于 Storage/Event Hubs bindings)。
TrustServerCertificate=trueFUNCTIONS_WORKER_RUNTIMEdotnet-isolatednodepythonpowershelljavaConnectionStringSettingconnectionStringSettingconnection_string_settingConnectionStep 2: install the SQL extension
第 2 步:安装 SQL 扩展
-
.NET isolated worker: add the NuGet package.bash
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql -
JavaScript / TypeScript / Python / PowerShell / Java: use the extension bundle in(Java also adds the
host.jsonMaven package):azure-functions-java-library-sqljson{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.0.0, 5.0.0)" } }
-
.NET isolated worker: 添加 NuGet 包。bash
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql -
JavaScript / TypeScript / Python / PowerShell / Java: 在中使用扩展捆绑包(Java 还需添加
host.jsonMaven 包):azure-functions-java-library-sqljson{ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.0.0, 5.0.0)" } }
Step 3: HTTP API with SQL input/output bindings
第 3 步:使用 SQL input/output bindings 构建 HTTP API
Template: choose one supported worker runtime. This example uses , but Node.js,
Python, and other runtimes are also available.
dotnet-isolatedbash
func init MyApi --worker-runtime dotnet-isolated
cd MyApi || exit 1
func new --name Books # pick an HTTP trigger templateThen wire the SQL bindings into the function. Per-language snippets (HTTP GET via
input binding, HTTP POST upsert via output binding) are in
references/functions-snippets.md; open it once you know your
language. Open references/functions-bindings-reference.md
when a binding attribute or a field is rejected.
function.jsonOutput-binding requirements: the target table must have a primary key
(the binding upserts via ), and the database compatibility level must
be 130+ (the binding uses ). The engine is fully capable; just
ensure the table has a PK.
MERGEOPENJSONRun it:
bash
func start # HTTP endpoints on http://localhost:7071/api/<name>模板:选择一种受支持的 worker runtime。此示例使用 ,但也可以使用 Node.js、Python 和其他运行时。
dotnet-isolatedbash
func init MyApi --worker-runtime dotnet-isolated
cd MyApi || exit 1
func new --name Books # pick an HTTP trigger template接下来,将 SQL bindings 接入函数。按语言划分的代码片段(通过 input binding 实现 HTTP GET,通过 output binding 实现 HTTP POST upsert)位于 references/functions-snippets.md;确定所用语言后再打开它。当 binding 属性或 字段被拒绝时,请打开 references/functions-bindings-reference.md。
function.jsonOutput binding 要求:目标表必须具有主键(binding 通过 执行 upsert),并且数据库兼容级别必须为 130+(binding 使用 )。引擎完全具备所需能力;只需确保表具有 PK。
MERGEOPENJSON运行:
bash
func start # HTTP endpoints on http://localhost:7071/api/<name>Step 4: event-driven with the SQL trigger (the local mechanism)
第 4 步:使用 SQL trigger 实现事件驱动(本地机制)
The SQL trigger fires your function when rows change.
There is no SQL trigger template to scaffold from, even though the tooling
lists one. With Azure Functions Core Tools 4.12.0, advertises a
entry, but
exits non-zero with
: the listing and the scaffolder disagree, and no
flag reconciles them. Scaffold an HTTP trigger instead (, which succeeds in the same project) and
write the trigger binding in by hand, as below.
func templates listSQL Triggerfunc new --template SqlTriggerUnknown template 'SqlTrigger'func new --name ToDoTrigger --template HttpTriggerThe trigger requires Change
Tracking on the database and table. Enable it once (on , not ):
appdbmastersql
ALTER DATABASE appdb
SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
ALTER TABLE dbo.ToDo ENABLE CHANGE_TRACKING;The function binds to a list of changes, each with an and an
( / / ). C# isolated example:
ItemOperationInsertUpdateDeletecsharp
[Function("ToDoTrigger")]
public static void Run(
[SqlTrigger("[dbo].[ToDo]", "SqlConnectionString")]
IReadOnlyList<SqlChange<ToDoItem>> changes,
FunctionContext context)
{
foreach (var change in changes)
context.GetLogger("ToDoTrigger")
.LogInformation($"{change.Operation}: {change.Item.Id}");
}The trigger creates an internal schema plus a
table (it makes these itself if the principal
can). Behavior, permission grants, and the CES-is-cloud-only detail are in
references/event-driven.md; open it when configuring the trigger or
its least-privilege grants. Since you connect as locally, the permission grants are already
satisfied; they matter when you move to least privilege or to the cloud.
az_funcLeases_{FunctionId}_{TableId}saSQL trigger 会在行发生变更时触发你的函数。
尽管工具列出了 SQL trigger 模板,但实际上没有可用于搭建的模板。 在 Azure Functions Core Tools 4.12.0 中, 会显示一个 条目,但执行 时会以非零状态退出并显示 :模板列表与脚手架生成器不一致,并且没有任何参数可以消除这种不一致。请改为搭建 HTTP trigger(,该命令在同一项目中可以成功执行),然后如下手动编写 trigger binding。
func templates listSQL Triggerfunc new --template SqlTriggerUnknown template 'SqlTrigger'func new --name ToDoTrigger --template HttpTriggerTrigger 要求在数据库和表上启用 Change Tracking。请在 (而不是 )上一次性启用:
appdbmastersql
ALTER DATABASE appdb
SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
ALTER TABLE dbo.ToDo ENABLE CHANGE_TRACKING;函数绑定到变更列表,每项包含一个 和一个 ( / / )。C# isolated 示例:
ItemOperationInsertUpdateDeletecsharp
[Function("ToDoTrigger")]
public static void Run(
[SqlTrigger("[dbo].[ToDo]", "SqlConnectionString")]
IReadOnlyList<SqlChange<ToDoItem>> changes,
FunctionContext context)
{
foreach (var change in changes)
context.GetLogger("ToDoTrigger")
.LogInformation($"{change.Operation}: {change.Item.Id}");
}Trigger 会创建内部的 schema 以及 表(如果主体具备相应权限,它会自行创建这些对象)。具体行为、权限授予以及 CES 仅适用于云端的详细信息见 references/event-driven.md;配置 trigger 或其最小权限授权时,请打开该文档。由于本地使用 身份连接,权限授予条件已经得到满足;迁移到最小权限配置或云端时,这些授权才会发挥作用。
az_funcLeases_{FunctionId}_{TableId}saValidation rules
验证规则
- The database engine is the container image above (EngineEdition=5), never
.
mcr.microsoft.com/mssql/server - exists (created on a master connection) before the function app runs;
appdbusesSqlConnectionStringandDatabase=appdb.TrustServerCertificate=true - The connection string lives in (or app settings), not in code; bindings reference it via
local.settings.json/ConnectionStringSetting.connectionStringSetting - Output-binding target tables have a primary key; the database compat level is 130+.
- The SQL trigger has Change Tracking enabled on both the database and the table; event-driven is done with the trigger locally, not CES.
- If a validation rule above fails, or you had to deviate from this skill to make the task work, that is a bug in this skill: load the azuresql-db-feedback skill and offer to file a report.
- When every rule above passes and the task is done, close with ONE line, once per session, and do not ask a question or wait for a reply: "That worked. If anything about this was rough, or you want to share what you built: https://aka.ms/sql-agent-skills-feedback"
- 数据库引擎必须是上述容器镜像(EngineEdition=5),绝不能使用 。
mcr.microsoft.com/mssql/server - 在函数应用运行前,必须已经存在(通过 master 连接创建);
appdb使用SqlConnectionString和Database=appdb。TrustServerCertificate=true - 连接字符串必须位于 (或应用设置)中,而不能写在代码里;bindings 通过
local.settings.json/ConnectionStringSetting引用它。connectionStringSetting - Output binding 的目标表必须具有主键;数据库兼容级别必须为 130+。
- SQL trigger 必须在数据库和表上同时启用 Change Tracking;本地事件驱动使用 trigger,而不是 CES。
- 如果上述任一验证规则失败,或你不得不偏离此 skill 才能让任务正常运行,则说明此 skill 存在 bug:加载 azuresql-db-feedback skill,并主动提出提交报告。
- 当上述所有规则均通过且任务完成时,每个会话只以以下一行结束一次,不要提问或等待回复:"That worked. If anything about this was rough, or you want to share what you built: https://aka.ms/sql-agent-skills-feedback"
Check it worked
验证是否成功
- Positive verification: Insert and update a tracked row, confirm the SQL trigger receives the event, then invoke the HTTP function and verify the output row.
- Cleanup verification: Stop the Functions host and remove test rows, generated state tables, and temporary local settings.
- Report success only after both observations pass.
- 正向验证: 插入并更新一条受跟踪的行,确认 SQL trigger 收到事件,然后调用 HTTP 函数并验证输出行。
- 清理验证: 停止 Functions host,并删除测试行、生成的状态表和临时本地设置。
- 只有在两项观察均通过后才能报告成功。
Do not
禁止事项
- Do not use the SQL Server image ; this is the Azure SQL engine.
mcr.microsoft.com/mssql/server - Do not expect the function app to create ; provision it on a master connection first.
appdb - Do not try to make Change Event Streaming (CES) work locally - it is unsupported on the local (Linux) engine and streams only to Azure Event Hubs. Use the SQL trigger locally.
- Do not use an output binding against a table with no primary key, or below compatibility level 130.
- Do not scaffold with ; the template is listed but cannot be created. Create an HTTP trigger and add the binding by hand.
func new --template SqlTrigger - Do not use the binding keyword for SQL bindings; it is
Connection/ConnectionStringSetting.connectionStringSetting - Do not commit (it holds the connection string / SA password) or drop
local.settings.json/TrustServerCertificate=trueon a non-x64 host.--platform linux/amd64
- 不要使用 SQL Server 镜像 ;这是 Azure SQL 引擎。
mcr.microsoft.com/mssql/server - 不要指望函数应用创建 ;请先通过 master 连接进行预配。
appdb - 不要尝试让 Change Event Streaming(CES)在本地运行——本地(Linux)引擎不支持它,并且它仅可流向 Azure Event Hubs。本地请使用 SQL trigger。
- 不要对没有主键或兼容级别低于 130 的表使用 output binding。
- 不要使用 搭建项目;虽然模板会被列出,但无法创建。请创建 HTTP trigger,然后手动添加 binding。
func new --template SqlTrigger - 不要对 SQL bindings 使用 binding 关键字;应使用
Connection/ConnectionStringSetting。connectionStringSetting - 不要提交 (其中包含连接字符串和 SA 密码),也不要在非 x64 主机上删除
local.settings.json或TrustServerCertificate=true。--platform linux/amd64
References
参考资料
- references/functions-bindings-reference.md: open it when you need binding fields, the setting, or
SqlConnectionStringtrigger tuning.host.json - references/functions-snippets.md: open it when you need copyable project setup, per-language function bodies, commands, or a local verification loop.
func - references/event-driven.md: open it when implementing a SQL trigger, granting its permissions, or replacing cloud-only CES locally.
- references/functions-bindings-reference.md:需要 binding 字段、设置或
SqlConnectionStringtrigger 调优时打开。host.json - references/functions-snippets.md:需要可复制的项目设置、按语言划分的函数体、命令或本地验证循环时打开。
func - references/event-driven.md:实现 SQL trigger、授予其权限或在本地替代仅支持云端的 CES 时打开。
Staying current
保持内容最新
Authoritative, version-pinned references for the tools this skill uses (read the one you need):
- Azure SQL bindings for Functions: input/output/trigger bindings, extension install, and connection settings.
- host.json reference: extensionBundle and all host.json top-level properties.
- host.json JSON schema: the authoritative schema for host.json.
- About change tracking: the change-tracking feature the SQL trigger depends on.
If the Microsoft Learn MCP server is configured, use or to fetch the current version of any of these on demand. It is optional; when it is unavailable, the references above are authoritative.
mcp__microsoft-learn__microsoft_docs_searchmcp__microsoft-learn__microsoft_docs_fetch此 skill 所用工具的权威且版本固定的参考资料(按需读取所需的一份):
- Azure Functions 的 Azure SQL bindings:input/output/trigger bindings、扩展安装和连接设置。
- host.json reference:extensionBundle 和所有 host.json 顶层属性。
- host.json JSON schema:host.json 的权威 Schema。
- About change tracking:SQL trigger 所依赖的 change-tracking 功能。
如果已配置 Microsoft Learn MCP server,请使用 或 按需获取上述资料的当前版本。这是可选项;不可用时,以上参考资料即为权威来源。
mcp__microsoft-learn__microsoft_docs_searchmcp__microsoft-learn__microsoft_docs_fetch