pay attention to this setting in the appsettings section :
FUNCTIONS_EXTENSION_VERSION=1.0.10690
#load "member.csx" | |
using System.Data.Entity; | |
using Microsoft.Azure; | |
public class AppContext: DbContext { | |
public static string connectionString = CloudConfigurationManager.GetSetting("SqlAzureConnectionString"); | |
// we should have an appSetting named SqlAzureConnectionString | |
// or simply use it in connectionStrings section and use this method | |
// ConfigurationManager.ConnectionStrings["ConnectStringToUse"].ConnectionString | |
public AppContext(): base(connectionString) | |
{ | |
} | |
public DbSet<Member> Members { get; set; } | |
} |
public class Member { | |
public int Id { get; set; } | |
public string Firstname { get; set; } | |
public string LastName { get; set; } | |
public string EmailAddress { get; set; } | |
} |
{ | |
"frameworks": { | |
"net46": { | |
"dependencies": { | |
"EntityFramework": "6.2.0", | |
"Microsoft.WindowsAzure.ConfigurationManager": "3.2.3" | |
} | |
} | |
} | |
} |
#r "Newtonsoft.json" | |
#r "System.Data.Entity" | |
#load "AppDataContext.csx" | |
#load "member.csx" | |
using System.Data.Entity; | |
using System.Net; | |
using System.Net.Http; | |
using Newtonsoft.Json; | |
const string GET = nameof(GET); | |
const string POST = nameof(POST); | |
const string PUT = nameof(PUT); | |
const string DELETE = nameof(DELETE); | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
var member = await req.Content.ReadAsAsync<Member>(); | |
var kp = req.GetQueryNameValuePairs() | |
.FirstOrDefault(p => String.Compare(p.Key, "id", true) == 0) | |
.Value; | |
var id = kp != null ? int.Parse(kp) : 0; | |
using(var context = new AppDataContext()){ | |
switch(req.Method.ToString()){ | |
case GET: { | |
var members = context.Members.ToList(); | |
return req.CreateResponse(HttpStatusCode.OK, (IEnumerable<Member>)members); | |
} | |
case POST: { | |
context.Set<Member>().Add(member); | |
await context.SaveChangesAsync(); | |
return req.CreateResponse(HttpStatusCode.Created, member); | |
} | |
case PUT: { | |
var current = context.Set<Member>().FirstOrDefault(m => m.Id == id); | |
if(current != null){ | |
context.Entry(current).CurrentValues.SetValues(member); | |
await context.SaveChangesAsync(); | |
} | |
return req.CreateResponse(HttpStatusCode.OK, member); | |
} | |
default: { | |
return req.CreateResponse(HttpStatusCode.OK, "reason"); | |
} | |
} | |
} | |
return req.CreateResponse(HttpStatusCode.BadRequest, "reason"); | |
} |