entity-framework-4 – 通过扩展方法的IDbSet和公开包含方法

我使用EF的Code-First方法,我想使用IDbSet而不是DbSet,所以我可以用嘲讽单元测试。我的问题是在需要时使用Include()方法来加载,但是不能通过IDbSet来显示Include()。我看到一个使用扩展方法显示Include()的示例代码,但它似乎不适用于我;此示例中的objectQuery对象始终为空。请让我知道如何解决这个问题。
public static class IQueryableExtension
{
    public static IQueryable<T> Include<T>(this IQueryable<T> source,string path)
        where T : class
    {
        ObjectQuery<T> objectQuery = source as ObjectQuery<T>;
        if (objectQuery != null)
        {
            return objectQuery.Include(path);
        }
        return source;
    }

    public static IQueryable<T> Include<T,TProperty>(this IQueryable<T> source,System.Linq.Expressions.Expression<Func<T,TProperty>> path)
        where T : class
    {
        ObjectQuery<T> objectQuery = source as ObjectQuery<T>;
        if (objectQuery != null)
        {
            return source.Include(path);
        }
        return source;
    }
}

解决方法

如果您使用CTP5,则无需编写此扩展名。 CTP5 provides包含IQueryable的扩展名。您必须参考EntityFramework.dll(CTP5)并添加
using System.Data.Entity;

您的版本可能无法正常工作,因为您将源转换为ObjectQuery,但最有可能是DbQuery类型。

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....