本文介绍了底层连接在使用时关闭错误。包含EF对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码行给了我一个错误,表示底层连接已关闭。

Following line of code gives me an error saying "The underlying connection was closed".

return this.repository.GetQuery<Countries>().Include(g => g.Cities).AsEnumerable().ToList();

但是,如果我删除 .Include(g => g.cities) 它工作正常。

But if I remove .Include(g => g.cities) it works fine.

这段代码写在我的WCF服务中的一个操作中,我尝试使用WCF测试客户端。我也尝试从MVC应用程序调用这个操作,同样的问题也发生在那里。
另外,我正在使用具有实体框架的通用存储库

this code is written in one of the operation in my WCF service, and I try to test it using WCF test client. I tried by calling this operation from MVC application also, and the same issue was occurring there too.Also, i am using generic repository with entity framework

存储库代码(只有几个重要的提取)

Repository code (only few important extract)

构造函数:

public GenericRepository(DbContext objectContext)
        {
            if (objectContext == null)
                throw new ArgumentNullException("objectContext");
            this._dbContext = objectContext;

            this._dbContext.Configuration.LazyLoadingEnabled = false;
            this._dbContext.Configuration.ProxyCreationEnabled = false;
        }

GetQuery方法:

GetQuery method:

public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {
        var entityName = GetEntityName<TEntity>();
        return ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
    }

尝试#1
创建以下存储库代码重载:

Attempt#1Created following overloads in repository code:

public IQueryable<TEntity> GetQuery<TEntity>(params string[] includes) where TEntity : class
        {
            var entityName = GetEntityName<TEntity>();
            IQueryable<TEntity> query = ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
                foreach(string include in includes)
                {
                    query = query.Include(include);
                }
            return query;
        }

public IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate, params string[] includes) where TEntity : class
        {
            return GetQuery<TEntity>(includes).Where(predicate);
        }

WCF正在尝试执行以下代码行:

WCF is now trying to execute following line of code:

return this.repository.GetQuery<Countries>("Cities").AsEnumerable().ToList()

但它仍然提供相同的错误底层连接已关闭。我在WCF测试客户端测试了它。但是,当我调试存储库代码时,它显示导航对象包含在结果中,但是当尝试将输出传递给客户端(WCF测试客户端或任何其他客户端)时,似乎出现了问题。

But it still gives the same error of "The underlying connection was closed". I tested it in WCF test client. However, when I debug the repository code it shows the navigation object getting included in result, but the issue seems occurring while trying to pass the output to client (WCF test client, or any other client)

推荐答案

在查看您现在发布的代码后,我可以得出结论,确实,您的DbContext在GetQuery方法结束时被关闭,因此尝试使用包含时失败。您可能想要解决的是为 GetQuery 方法选择一个可选的 params 变量一些属性被包括在内,并且只需在 GetQuery 方法本身中进行包含。

After looking at the code you've now posted, I can conclude that, indeed, your DbContext is being closed at the end of the GetQuery method, and is thus failing when you try to use include. What you might want to do to solve it is to have an optional params variable for the GetQuery method that will take in some properties to be included, and just do the include right in the GetQuery method itself.

这篇关于底层连接在使用时关闭错误。包含EF对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:13