使用Include(string)方法包含多个导航属性

时间:2015-10-16 18:12:24

标签: c# entity-framework iqueryable navigation-properties

首先,我试图避免直接链接到我的程序集中的EntityFramework,因此我无法在客户端代码中使用System.Data.Entity命名空间,仅在接口实现类中

我有界面

    public interface IEntitySource<T>
        where T : class
    {
        ...
        IQueryable<T> WithProperties(params string[] properties);
        ...
    }

以及它的EF实施:

public class EfEntitySource<T> : IEntitySource<T>
    where T : class
{
    public IQueryable<T> WithProperties(params string[] properties)
    {
        IQueryable<T> queryable = this.DbSet;
        foreach (var property in properties)
        {
            queryable = this.DbSet.Include(property);
        }

        return queryable;
    }
}

和客户端代码:

public IEnumerable<ContentChangeHistory> GetContentActivityAtGroup(Guid groupId)
{
    var groupActivity = this.ContentChangeHistorySource
        .WithProperties("ContentPost", "ContentItem", "SystemUser")
        .Where(cch => cch.ChangeGroupId == groupId);
    return groupActivity;
}

但是,执行GetContentActivityAtGroup方法的代码返回ContentChangeHistory集合,只初始化了最新的导航属性,例如SystemUser

一些代码修改,如下所示:

public IQueryable<T> WithProperties(params string[] properties)
{
    foreach (var property in properties)
    {
        this.DbSet.Include(property);
    }

    return this.DbSet;
}

没有结果

1 个答案:

答案 0 :(得分:1)

更改

queryable = this.DbSet.Include(property);

queryable = queryable.Include(property);