How to insert multiple objects into an Azure Mobile Services table controller [.Net backend]

I have an Azure Mobile service coded in a .net Web Api. I have a TableController. I want this table controller to be able to insert multiple people, not just one person with a client using InsertAsync (myPerson). I have the following code in a TableController:

    [RequiresAuthorization(AuthorizationLevel.Admin)]        
    public async Task<bool> InsertPersons(List<Person> values)
    {
        try
        {
            foreach (var item in values)
            {                   
                var current = await InsertAsync(item);
            }

            return true;
        }
        catch (System.Exception)
        {
            return false;
        }

    }

      

The problem is in the client. Since it prints heavily, it only allows me to insert one element at a time. How can I call the server from the client? Should I write a custom Api Controller and call it mobileService.InvokeApiAsync

? If so, how can I access my database from a custom Api Controller that does not inherit from TableController?

Thank you very much!

+3


source to share


2 answers


The helper methods in the base class TableController<T>

assume that insert operations are applied to a single object — and the InsertAsync method in the client also assumes the same. So even if you can define a method in the table controller that accepts an array (or list) of Person, you cannot call it through the client SDK (at least not without some heavy lifting with a handler, for example).

However, you can create a custom API that accepts such a list. And to insert multiple elements from the API, you can access the context directly, without having to go through the helper methods from the table:

public class PersonController : ApiController
{
    test20140807Context context;

    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        this.context = new test20140807Context();
    }

    [HttpPost]
    public async Task<bool> InsertPersons(List<Person> values)
    {
        foreach (var value in values)
        {
            if (string.IsNullOrEmpty(value.Id))
            {
                value.Id = Guid.NewGuid().ToString();
            }
        }

        try
        {
            this.context.People.AddRange(values);
            await this.context.SaveChangesAsync();

            return true;
        }
        catch (System.Exception ex)
        {
            Trace.WriteLine("Error: " + ex);
            return false;
        }
    }
}

      



And on the client:

private async void btnTest_Click(object sender, RoutedEventArgs e)
{
    var items = new Person[]
    {
        new Person { Name = "John Doe", Age = 33 },
        new Person { Name = "Jane Roe", Age = 32 }
    };
    try
    {
        var response = await App.MobileService.InvokeApiAsync<Person[], bool>("person", items);
        Debug.WriteLine("response: " + response);
    }
    catch (Exception ex)
    {
        var str = ex.ToString();
        Debug.WriteLine(str);
    }
}

      

+9


source


From Carlos Figueira's post on inserting multiple elements at once in azure mobile services , it looks like what you need to do is create another table called AllPersons. In your client, the AllPersons object will have a member of the Persons array. On the server side script for inserting AllPersons you iterate through AllPersons.Persons and insert into the table one by one.



0


source







All Articles