2

当我选择文章时,它选择了用户,但用户有一个文章的集合,所以文章再次选择用户。可能是递归导致内存不足,调用处理为:article=>user=>article=>user...

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操作类是:

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

}

如何避免急切加载集合属性角色用户和文章?

4

1 回答 1

0

您需要在 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;
}

你需要在另一个类中实现它,它可以是一个部分类。

于 2013-10-01T15:54:12.120 回答