forked from Azure/azure-functions-sql-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddProductWithIdentityColumn.cs
42 lines (38 loc) · 1.76 KB
/
AddProductWithIdentityColumn.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples
{
public class ProductWithoutId
{
public string Name { get; set; }
public int Cost { get; set; }
}
public static class AddProductWithIdentityColumn
{
/// <summary>
/// This shows an example of a SQL Output binding where the target table has a primary key
/// which is an identity column. In such a case the primary key is not required to be in
/// the object used by the binding - it will insert a row with the other values and the
/// ID will be generated upon insert.
/// </summary>
/// <param name="req">The original request that triggered the function</param>
/// <param name="product">The created Product object</param>
/// <returns>The CreatedResult containing the new object that was inserted</returns>
[FunctionName(nameof(AddProductWithIdentityColumn))]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "addproductwithidentitycolumn")]
HttpRequest req,
[Sql("dbo.ProductsWithIdentity", ConnectionStringSetting = "SqlConnectionString")] out ProductWithoutId product)
{
product = new ProductWithoutId
{
Name = req.Query["name"],
Cost = int.Parse(req.Query["cost"])
};
return new CreatedResult($"/api/addproductwithidentitycolumn", product);
}
}
}