Azure Mobile Services C# won't return child entities

我只是一个虾纸丫 提交于 2019-11-29 04:19:23

My mistake was not really putting the OData part into the equation. As soon as I queried

http://localhost:51025/tables/todoitem?$expand=Numbers

instead of

http://localhost:51025/tables/todoitem

It magically appeared. Now how do I get that to come across in the client API...? Update I managed it on the client side with this code

var json =
                    await
                        App.MobileService.GetTable("TodoItem")
                            .ReadAsync("$expand=Numbers");
JsonConvert.DeserializeObject<TodoItem>(json.First.ToString());

But there has to be a better way

It is also possible to achieve it by adding a handler on the client:

var client = new MobileServiceClient("http://xxxxxx/", null, new ODataParameterHandler());

public class ODataParameterHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        UriBuilder builder = new UriBuilder(request.RequestUri);

        builder.Query = builder.Query
            .Replace("expand", "$expand")
            .TrimStart('?');

        request.RequestUri = builder.Uri;

        return await base.SendAsync(request, cancellationToken);
    }
}

Usage:

var test = await client.GetTable<TodoItem>()
    .WithParameters(new Dictionary<string, string> { { "expand", "Something" } })
    .ToListAsync();

Instead of overriding the Query method, have the GetAllTodoItems method look like this:

public IList<TodoItem> GetAllTodoItems()
{
    return context.TodoItems.Include( "Numbers" ).ToList();
}

This is an alternative to the $expand hack. I discovered it while exploring Azure App Service mobile apps for myself. Chaining on an Include call alone, as you originally attempted, would make more sense to me if it worked. Next, I'd like to figure out why that doesn't work.

I thought it was .Include(x => x.Numbers) after the gettable

Supreet

I had the same issue & using expand did help. Then a @carlosfigueira suggested a better way of handling this scenario here

How do we load related objects (Eager Loading) in Dot Net based Azure Mobile Service?

Please have a look. So far the best approach to tackle this issue. Supreet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!