简体   繁体   中英

Azure Mobile Services Tablecontroller updateAsync ends up in out of memory exception

When I am running a patch method which runs updateAsync(id,patch) ,I end up in what I think is a infinite reference loop which then stops with the server crashing with out of memory exception.

So I have the models

  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; }
}

and the tablecontroller patch action looks like this

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

So I try to patch Bars enumstatus and then it feels like it starts looping through all related entites and starts updating them as well. How can I solve this? Maybe I should rethink my inheritance

Update 1: After further investigation it seems to load all the related entities without me asking for it. Why is this happening?

As fun as lazy loading is, it can be dangerous for this reason. I personally prefer to disable lazy loading and use the IQueryable<T>.Include method to include only the related entities relevant to the query, otherwise you can accidentally end up pulling your whole database. You can turn off lazy loading in the MobileServiceContext constructor:

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

You can find a decent example of using IQueryable<T>.Include() to accomplish eager loading here .

Hopefully this is what you are looking for!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM