基于Azure的移动业务点网我们如何加载相关对象(预先加载)? [英] How do we load related objects (Eager Loading) in Dot Net based Azure Mobile Service?

查看:155
本文介绍了基于Azure的移动业务点网我们如何加载相关对象(预先加载)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下的模型结构

If I have following model structure

public class QuestionItem: EntityData
{
    public string Content { get; set; }
    public bool IsAnswered { get; set; }
    public int NumberOfAnswers
    {
        //todo: make it computable
        get;
        set;
    }
    public UserItem By { get; set; }
    public string ById { get; set; }
    public string AtLocation { get; set; }
}

&放大器;母公司为

& parent entity as

public class UserItem:EntityData
{

    public string UserName { get; set; }

    public string Gender { get; set; }

    public string BaseLocation { get; set; }

    public int Age { get; set; }

    public int Points { get; set; }

}

这确实生成数据库表适当的FK关系。但查询问题的项目列表时,该QuestionItem.By属性(引用对象UserItem表)为空。

this does generate the DB Tables with proper FK relationships. but when querying Question Items list, the QuestionItem.By property (reference object to UserItem table) is null.

通常这是通过使用查询级的.INCLUDE方法来实现,但我无法找到确切位置在哪里我应该这样做。

generally this is achieved by using an .Include method on query level, but i am not able to find where exactly i should do this.

任何帮助是AP preciated。

any help is appreciated.

苏$ P $宠物

推荐答案

由于@JuneT提到的,你需要从客户端发送一个 $展开头。这其中的原因是,默认情况下,实体框架不会遍历对象图,因为这需要一个连接在数据库中,可以产生负面影响性能,如果你不需要这么做。

As @JuneT mentioned, you need to send an $expand header from the client. The reason for that is that by default Entity Framework will not traverse object graphs, as this requires a join in the database and can have a negative performance impact if you don't need to do that.

另一种选择,这是我在<一提到href="http://blogs.msdn.com/b/azuremobile/archive/2014/04/14/creating-mongodb-backed-tables-in-azure-mobile-services-with-net-backend.aspx"相对=nofollow>这篇博客,就是使用过滤器在服务器端添加了标题为您服务。例如,对于低于这个属性:

Another alternative, which I mentioned in this blog post, is to use a filter in the server-side to add that header for you. For example, with this attribute below:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
class ExpandPropertyAttribute : ActionFilterAttribute
{
    string propertyName;

    public ExpandPropertyAttribute(string propertyName)
    {
        this.propertyName = propertyName;
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
        var uriBuilder = new UriBuilder(actionContext.Request.RequestUri);
        var queryParams = uriBuilder.Query.TrimStart('?').Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        int expandIndex = -1;
        for (var i = 0; i < queryParams.Count; i++)
        {
            if (queryParams[i].StartsWith("$expand", StringComparison.Ordinal))
            {
                expandIndex = i;
                break;
            }
        }

        if (expandIndex < 0)
        {
            queryParams.Add("$expand=" + this.propertyName);
        }
        else
        {
            queryParams[expandIndex] = queryParams[expandIndex] + "," + propertyName;
        }

        uriBuilder.Query = string.Join("&", queryParams);
        actionContext.Request.RequestUri = uriBuilder.Uri;
    }
}

您可以装饰你的行动,迫使相关实体的扩张。

You can decorate your action to force the expansion of the related entity.

[ExpandProperty("By")]
public IQueryable<QuestionItem> GetAll() {
    return base.Query();
}

这篇关于基于Azure的移动业务点网我们如何加载相关对象(预先加载)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆