【问题标题】:Include several navigation properties with Include(string) method使用 Include(string) 方法包含几个导航属性
【发布时间】:2015-10-16 18:12:23
【问题描述】:

首先,我试图避免在我的程序集中直接链接到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;
}

没有结果

【问题讨论】:

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


    【解决方案1】:

    改变

    queryable = this.DbSet.Include(property);
    

    queryable = queryable.Include(property);
    

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多