【问题标题】:Azure Mobile Services Tablecontroller updateAsync ends up in out of memory exceptionAzure 移动服务表控制器 updateAsync 出现内存不足异常
【发布时间】:2016-08-08 07:38:44
【问题描述】:

当我运行一个运行 updateAsync(id,patch) 的补丁方法时,我最终会陷入我认为是无限引用循环,然后随着服务器因内存不足异常而崩溃而停止。

所以我有模型

  public class User : EntityData
{
    public string Username { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
    public virtual ICollection<Foo> Foos { get; set; }
}

public class Bar: EntityData
{
    public string FooId { get; set; }
    public string UserId { get; set; }
    public enum enumStatus { get; set; }
    public virtual Foo Foo { get; set; }
    public virtual User User { get; set; }
} 

public class Foo: EntityData
{
   public string Title { get; set; }
   public string UserId { get; set; }
   public virtual ICollection<Bar> Bars { get; set; }
   public virtual User User { get; set; }
}

tablecontroller 补丁操作看起来像这样

public Task<Bar> PatchInvited(string id, Delta<Bar> patch)
{
     return UpdateAsync(id, patch);
}

所以我尝试修补 Bars 枚举状态,然后感觉它开始循环遍历所有相关实体并开始更新它们。 我该如何解决这个问题?也许我应该重新考虑我的继承权

更新 1:经过进一步调查,它似乎在没有我要求的情况下加载了所有相关实体。为什么会这样?

【问题讨论】:

  • 相关实体被加载为您的Navigation Properties 被标记为virtual 即延迟加载。我建议使用Data Transfer Objects 映射服务所需的正确数据,而不是加载所有内容(缓慢、无限循环等)。您有相关的exception 消息吗?
  • 好的,所以制作 DTO 解决了它,但我不明白为什么它会加载所有虚拟数据?制作 DTO 会打破使用 ODATA 的想法?

标签: c# azure inheritance asp.net-web-api azure-mobile-services


【解决方案1】:

虽然延迟加载很有趣,但由于这个原因它可能很危险。我个人更喜欢禁用延迟加载并使用IQueryable&lt;T&gt;.Include 方法仅包含与查询相关的相关实体,否则您可能会意外地拉取整个数据库。您可以在 MobileServiceContext 构造函数中关闭延迟加载:

    public MobileServiceContext() : base(connectionStringName)
    {
        Configuration.LazyLoadingEnabled = false;
        Configuration.ProxyCreationEnabled = false;
    }

您可以找到一个使用IQueryable&lt;T&gt;.Include() 来完成即时加载here 的体面示例。

希望这就是您要找的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2010-09-13
    相关资源
    最近更新 更多