我有以下Azure函数
[FunctionName("AllProducts")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "marketplace",
collectionName: "products",
SqlQuery = "SELECT * FROM product",
ConnectionStringSetting = "CosmosDbConnectionString")] IEnumerable<Product> products,
ILogger log)
{
...some magic here
}
它运行在。net Core 3.1与Azure Functions runtime 3.x。Product.ID
属性为Guid
。当我向函数发送请求时,我得到500
,并在日志流中看到以下消息:
已执行'AllProducts' (Failed,)Id=147b3de4-c6f1-445c-80ba-28e75a25ed31,持续时间=48ms)无法施放或从系统转换。System.Guid.
是否有一种方法从字符串转换到guid而不改变我的模型?
模型类如下:
public class Product
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public bool IsAvailable { get; set; }
[ForeignKey("User")]
public string OwnerName { get; set; }
public User Owner { get; set; }
}
请确保您的Id属性与文档的自动生成Id属性不冲突。您可以在文档中将属性更改为PropertyID,并相应地序列化它
public class Product
{
[JsonProperty("PropertyID")]
public Guid Id { get; set; }
}
CosmosDB为每个文档自动生成ID。在我的例子中,自动生成的id看起来像<Discriminator>|<Auto-generated guid>
。这使得绑定无法解析它作为向导接收到的ID。我将Product.ID
属性更改为Product.ProductID
,之后它开始工作。