Entity framework RRS feed

  • Question

  • Hi,

    Is it possible to use entity framework with Functions? I have not seem any demo code on it.

    Would be possible to provide, please?


    Saturday, April 23, 2016 12:35 PM

All replies

  • I would think that it should work by importing the NuGet package (see documentation). If you run into specific issues, please post details.


    Saturday, April 23, 2016 4:07 PM
  • hello, 

    exactly you just have to add the reference to the EF nugget package, anyway i posted a gist as an example:

    first of all you have to add a file to your function files collection: 

    and then start adding your model in the example i created a file member.csx :

    public class Member {
        public int Id { get; set; }
        public string Firstname { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }

    and then add another file for the data context, i name it AppDataContext.csx (this time you have to load the POCO file previously created):

    #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; }

    and finally use it on your function : 

    // assemblies references
    #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;
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        var member = await req.Content.ReadAsAsync<Member>();
        var id_parameter = req.GetQueryNameValuePairs()
            .FirstOrDefault(p => String.Compare(p.Key, "id", true) == 0)
        var id = id_parameter != null ? int.Parse(id_parameter) : 0;
        using( var context = new AppContext()){
            if(req.Method == HttpMethod.Get) 
                var members = context.Members.ToList();
                return req.CreateResponse(HttpStatusCode.OK, (IEnumerable<Member>)members);
            else if(req.Method == HttpMethod.Post && id == 0) 
                await context.SaveChangesAsync();
                return req.CreateResponse(HttpStatusCode.Created, member);       
            else if((req.Method == HttpMethod.Post && id != 0) || req.Method == HttpMethod.Put) 
                var current = context.Set<Member>().FirstOrDefault(m => m.Id == id);
                if(current != null){
                    await context.SaveChangesAsync();
                return req.CreateResponse(HttpStatusCode.OK, member);       
        return req.CreateResponse(HttpStatusCode.BadRequest, "reason");       
    Note: to avoid Ado.Net Provider registration, you should go to the app setting section and replace the version of your FUNCTION_EXTENSION_VERSION=~1 with a correct version number such as FUNCTIONS_EXTENSION_VERSION=1.0.10690

    here is a gist containing the code :

    good coding;

    Monday, July 30, 2018 9:34 PM