Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Dec 22, 2016 @ 04:28
    Nathan Woulfe
    0

    Best practice with PetaPoco

    I'm looking at converting some old code from Entity Framework to PetaPoco, just need some advice/direction on how to best achieve this.

    Currently a single entity is returned with data from two custom tables - I need to replicate that using PetaPoco.

    eg

    Table 1 contains a group id (PK), name, alias and summary text. Table 2 contains a group id and user id (relating users to groups).

    How can my POCO return a model representing all the groups from table 1, with an enumerable object of Umbraco Users, derived from the user id Table 2, where the group id provides the foreign key?

    I'm fine with creating the tables dynamically, and storing or querying either table, just not sure how to use a single model to return data from two tables...

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Dec 22, 2016 @ 14:57
    Alex Skrypnyk
    0

    Hi Nathan,

    First of all I would recommend use one connection to the db per request, we made it with this static method:

    public static Database CurrentDb()
            {
                if (HttpContext.Current.Items["CurrentDb"] == null)
                {
                    var retval = new Database(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName);
                    HttpContext.Current.Items["CurrentDb"] = retval;
    
                    return retval;
                }
    
                return (Database)HttpContext.Current.Items["CurrentDb"];
            }
    

    Maybe it's not related to your question but it will from connection errors with Peta POCO.

    Thanks,

    Alex

  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Dec 23, 2016 @ 07:40
    Nathan Woulfe
    100

    Thanks Alex

    I'm fetching the db via ApplicationContext.Current.DatabaseContext.Database, no dramas there.

    Think for now I'll just wear the extra db call, and build my own response model. Overhead won't really matter as it's a back office extension rather than a front-end action

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies