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

标签 c# entity-framework iqueryable navigation-properties

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

我有界面

    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 集合,例如系统用户

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

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

    return this.DbSet;
}

没有给出结果

最佳答案

改变

queryable = this.DbSet.Include(property);

queryable = queryable.Include(property);

关于c# - 使用 Include(string) 方法包含多个导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176989/

相关文章:

c# - 从另一个 Controller 调用 WebApi Controller

c# - ProgressBar 在 Windows 窗体中很慢

嵌入到 .bat 文件中的 C# 源代码

c# - IQueryable 到底是什么意思?

enums - 如何在可查询投影期间使用 AutoMapper 将 int 映射到其枚举描述?

c# - 如何计算 gridview 中选定的复选框的数量

sql-server - 这种情况下最好的数据库结构是什么?

c# - 使用继承时设置外键关联的正确方法

.NET:抽象数据源和数据上下文

c# - 在一次数据库往返中执行两个查询