none
How to query from another table inside a TableController in a .NET backend Mobile Service RRS feed

  • Question

  • I have a .NET backend Azure Mobile Service and I have two tables. Inside the controller of one the tables I need to tun a query on the other one. How can I do that?

    For example let say I have two tablecontrollers:

        public class UserProfileController : TableController<UserProfile>

        public class TodoItemController : TableController<TodoItem>
        {
              ....

            public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
            {
                /**************************************
                  Here I want run some query from UserProfile table
                **************************************/
                TodoItem current = await InsertAsync(item);
                return CreatedAtRoute("Tables", new { id = current.Id }, current);
            }
        }

    Wednesday, March 26, 2014 6:47 PM

Answers

  • In the Inizialize method of the TableController, an Entity Framework DbContext is created and used to instantiate the DomainManager property.

    So, you can simply declare a class variable to hold the DbContext reference, and then use it everywhere you need. In this way, you can query every table exposed by the Entity Framework context.


    Marco Minerva - MVP Visual C#
    Blog: http://marcominerva.wordpress.com
    Twitter: @marcominerva

    • Marked as answer by Reza J Monday, March 31, 2014 5:57 AM
    Sunday, March 30, 2014 9:31 PM

All replies

  • In the Inizialize method of the TableController, an Entity Framework DbContext is created and used to instantiate the DomainManager property.

    So, you can simply declare a class variable to hold the DbContext reference, and then use it everywhere you need. In this way, you can query every table exposed by the Entity Framework context.


    Marco Minerva - MVP Visual C#
    Blog: http://marcominerva.wordpress.com
    Twitter: @marcominerva

    • Marked as answer by Reza J Monday, March 31, 2014 5:57 AM
    Sunday, March 30, 2014 9:31 PM
  • How about a quick code example? 
    Sunday, May 31, 2015 6:10 PM
  • In the Inizialize method of the TableController, an Entity Framework DbContext is created and used to instantiate the DomainManager property.

    So, you can simply declare a class variable to hold the DbContext reference, and then use it everywhere you need. In this way, you can query every table exposed by the Entity Framework context.


    Marco Minerva - MVP Visual C#
    Blog: http://marcominerva.wordpress.com
    Twitter: @marcominerva

    Thanks so much. Great! You saved me!
    Sunday, August 18, 2019 5:30 PM