Azure SQL Azure Functions 的输出绑定不会在数据库中插入行

Azure SQL output binding for Azure Functions will not insert row in database

我正在按照本指南创建绑定以更新我的数据库:https://docs.microsoft.com/da-dk/azure/azure-functions/functions-bindings-azure-sql-output?tabs=csharp

我觉得我已经按照指南所说的做了所有事情,但是我仍然遇到错误。

错误是:

[2022-03-10T20:39:31.466Z] Executed 'PostFileInformationByDeviceId' (Failed, Id=d0cecd1b-e54b-4716-9cfc-921bb6f458ac, Duration=701ms)
[2022-03-10T20:39:31.467Z] System.Private.CoreLib: Exception while executing function: PostFileInformationByDeviceId. Microsoft.Azure.WebJobs.Host: Error while handling parameter newItem after function returned:. Microsoft.Azure.WebJobs.Extensions.Sql: Encountered error(s) while parsing schema and object name: Incorrect syntax near ..

函数代码如下:

[FunctionName("PostFileInformationByDeviceId")]
public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "postFile/{deviceId}")]
HttpRequest req, [Sql("dbo.File", ConnectionStringSetting = "SqlConnectionString")] out FileInformation newItem,
    ILogger log)
{
    newItem = new FileInformation
    {
Hash = "hash",
DeviceId = "deviceid"
    };

    return new OkResult();
}

这是我的模型:

public class FileInformation
{
public string Hash { get; set; }

public string UserId { get; set; }

public string DeviceId { get; set; }

public int Timestamp { get; set; }

public string Filename { get; set; }

public int Filetype { get; set; }
}

我的数据库:

CREATE TABLE [dbo].[File] (
[Hash]      VARCHAR (20) NOT NULL,
[Timestamp] INT          NULL,
[DeviceId]  NCHAR (10)   NOT NULL,
[UserId]    NCHAR (10)   NULL,
[Filename]  NCHAR (10)   NULL,
[Filetype]  INT          NULL,
PRIMARY KEY CLUSTERED ([Hash] ASC)
);

我 purposley 将数据库字段设置为 NOT NULL 这样我就可以轻松地测试插入函数只用到值

非常感谢任何帮助。

“文件”是 T-SQL 中的保留字:

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver15

尝试将您的 table 命名为非保留字的名称 - 也许是“dbo.MyFile”。