关于 c#:Multiple 包括使用实体框架和存储库模式 | 珊瑚贝

Multiple includes using Entity Framework and Repository Pattern


我正在使用实体框架和存储库模式进行所有数据访问,当使用表格导航时,我注意到当我获得第一个对象并引用导航对象中的字段时正在运行 2 个查询。由于我在数据库中有很多关系,将这种技术用于我的导航属性可能会导致性能开销。

我已经研究了 Include(string tableName) 方法,它会很好地工作(如果我没有使用通用 RP),但这只需要一个表名。我已经设法通过将我的 where 从 classs 更改为 EntityObject 来在我的存储库模式中复制此内容,但是如何使用存储库模式在一个查询中包含多个包含??

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class GenericRepository< T > : IRepository< T > where T : EntityObject, new()
{
    private Entities _Context;
    private ObjectSet< T > _ObjectSet;

    public IQueryable< T > FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, string include)
    {
        // This works OK
        return this._ObjectSet.Include(include).Where(predicate);
    }

    public IQueryable< T > FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include)
    {
        // This will not work but is what I am trying to do
        return this._ObjectSet.Include(include).Where(predicate);
    }
}


您可以链接您的包含:

1
2
3
4
5
6
7
8
9
10
public IQueryable< T > FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include)
{
    IQueryable< T > query = this._ObjectSet;
    foreach(string inc in include)
    {
       query = query.Include(inc);
    }

    return query.Where(predicate);
}

  • 这不是编译,query = query.Include(inc) IQueryable< T > 不包含名为 Include 的方法。
  • 您可能需要添加一个 using System.Data.Entity
  • ObjectQuery<T> 也定义了方法,因此您可以用它替换 IQueryable<T>。你用的是什么版本的EF?
  • 我刚刚意识到它不存在,因为我使用的是旧版本的实体框架和 System.Data.Entity。我更新了版本,它现在可以工作了。
  • 旧版本是什么版本,你升级到什么版本?


来源:https://www.codenong.com/12625929/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9579.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?