2

When I select artile, it select user, but user has a collection of article, so article select user again. May be recursive cause out of memory , The calling processing is : article=>user=>article=>user...

ef entities is :

public partial class article
{
public int id { get; set; }
public string title { get; set; }
public string cont { get; set; }
public Nullable<int> uid { get; set; }
public System.DateTime addtime { get; set; }
public Nullable<int> colid { get; set; }

public virtual user user { get; set; }
public virtual column column { get; set; }
}

public partial class user
{
public user()
{
    this.roleusers = new HashSet<roleuser>();
    this.articles = new HashSet<article>();
}

public int id { get; set; }
public string email { get; set; }
public string uname { get; set; }
public string upass { get; set; }

public virtual ICollection<roleuser> roleusers { get; set; }
public virtual ICollection<article> articles { get; set; }
}

mysql EF operation class is :

public class ArtDao
{
    readonly crmEntities _ent = new crmEntities();
    public List<article> PageArts(int start, int limit, out int total)
    {

        var ll =
            _ent.articles.OrderByDescending(o => o.id)
                .Skip(start)
                .Take(limit)
                .ToList();
        total = _ent.articles.Count();
        return ll;
    }

}

How to avoid to eager load the collection property roleusers and articles ?

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
qizweb
  • 69
  • 4

1 Answers1

0

You need to set LazyLoad on you edmx properties, and manually load only first level childs with Include() method when selecting:

public List<article> PageArts(int start, int limit, out int total)
{

    var ll =
        _ent.articles.OrderByDescending(o => o.id)
            .Skip(start)
            .Take(limit)
            .Include(o => o.user)
            .ToList();
    total = _ent.articles.Count();
    return ll;
}

You need to implement it in another class, wich can be a partial class.

rkawano
  • 2,443
  • 22
  • 22
  • how to set LazyLoad on edmx properties? – qizweb Oct 01 '13 at 23:57
  • "You need to implement it in another class, wich can be a partial class." could you clarify ? – qizweb Oct 02 '13 at 00:05
  • why there is no Include method? – qizweb Oct 02 '13 at 00:09
  • To enable lazy load, just double click on your "Model.edmx" file on Solution pane, click on a blank area of the diagram and go to Properties pane. Look for a property named "Lazy Loading Enabled" on "Code Generation" group, and set it to True. If you have writed this method yourself then you don't need to create a extension class, but if this method was auto generated, then you will need it. – rkawano Oct 02 '13 at 01:56
  • I have set the "lazy loading enabled" tree. – qizweb Oct 02 '13 at 10:26
  • you mean i must write own Include method? could you please give me an example? where to write and how to write? my entity framework project is all auto generated. – qizweb Oct 02 '13 at 10:28
  • You can get some ideas here: http://stackoverflow.com/questions/17529796/what-is-the-best-practice-for-multiple-include-method-calls-in-entity-framework – rkawano Oct 02 '13 at 15:32