我挣扎与一个相当简单的函数,应该添加一个记录到Azure SQL Server表。下面的例子抛出:
System.Private。CoreLib:执行AddUser函数时出现异常。
Microsoft.Azure.WebJobs。主机:函数返回后处理参数newLicenses时出错。
Microsoft.Azure.WebJobs.Extensions。Sql:出现意外错误。核心Microsoft SqlClient数据提供程序:无法将值NULL插入到' downloadaddt '列,'_____.dbo.license'表;列不允许为空。更新失败。
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "put", Route = null)] HttpRequest req,
ILogger log,
[Sql("dbo.license", "SqlConnStr")] IAsyncCollector<License> newLicenses)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var userInfo = JsonConvert.DeserializeObject<AddLicenseBody>(requestBody);
var utcNow = DateTime.UtcNow;
var newLicense = new License()
{
emailKey = userInfo.emailAddress.ToLower(),
downloadDT = DateOnly.FromDateTime(utcNow),
};
log.LogInformation("inserting to dbo.license", newLicense);
await newLicenses.AddAsync(newLicense);
var licensesAdded = new List<AddLicenseBody> { userInfo };
return new OkObjectResult(licensesAdded);
}
public class License
{
public string emailKey { get; set; }
public DateOnly downloadDT { get; set; }
public DateOnly? installDT { get; set; }
public DateTime? lastverifyDT { get; set; }
public DateOnly? uninstallDT { get; set; }
public int? deactivated { get; set; }
public string deactivationReasonID { get; set; }
public DateOnly? expiryDT { get; set; }
}
SQL表是:
CREATE TABLE license
(
emailKey NVARCHAR(256) PRIMARY KEY,
downloadDT DATE NOT NULL,
installDT DATE,
lastverifyDT DATETIME,
uninstallDT DATE,
deactivated BIT,
deactivationReasonID NVARCHAR(256),
expiryDT DATE
)
怎么了?
好的,我发现了问题。似乎我不能使用DateOnly数据类型绑定SQL Server的数据。相反,我需要使用DateTime对象并使用它的。date属性:
// ...
var utcNow = DateTime.UtcNow;
var newLicense = new License()
{
emailKey = email,
downloadDT = utcNow.Date,
};
// ...
public class License
{
// ...
public DateTime downloadDT { get; set; }
// ...
}