简体   繁体   English

如何避免在Entity Framework中加载集合属性

[英]How to avoid to load collection property in Entity Framework

When I select artile, it select user, but user has a collection of article, so article select user again. 当我选择Artile时,它将选择用户,但是用户具有商品集合,因此商品再次选择了用户。 May be recursive cause out of memory , The calling processing is : article=>user=>article=>user... 可能是递归导致内存不足,调用处理是:article => user => article => user ...

ef entities is : ef实体是:

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 : mysql EF操作类为:

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 ? 如何避免急于加载集合属性roleusers和article?

You need to set LazyLoad on you edmx properties, and manually load only first level childs with Include() method when selecting: 您需要在edmx属性上设置LazyLoad,并在选择时使用Include()方法仅手动加载一级子级:

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. 您需要在另一个类中实现它,它可以是局部类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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