Sql server 无效操作。连接已关闭

Sql server 无效操作。连接已关闭,sql-server,entity-framework,unity-container,Sql Server,Entity Framework,Unity Container,我们正在一个刚刚上线的项目中使用EntityFramework。根据我们的日志,有几个用户正在遭受无效操作。连接已关闭,每天大约发生50次 异常情况如下所示: An error occurred while executing the command definition. See the inner exception for details. at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands

我们正在一个刚刚上线的项目中使用EntityFramework。根据我们的日志,有几个用户正在遭受无效操作。连接已关闭,每天大约发生50次

异常情况如下所示:

An error occurred while executing the command definition. See the inner exception for details.    at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at xxxx.SelectResultToPeople(IEnumerable`1 result) 
   at xxxx.GetResult()
   at DynamicModule.ns.Wrapped_IPeopleGetter_a853d13914444ca6ab0e016e035520c5.<People_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
Invalid operation. The connection is closed.    at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
   at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
   at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
   at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
   at System.Data.SqlClient.TdsParserStateObject.TryPeekByte(Byte& value)
   at System.Data.SqlClient.SqlDataReader.TrySetMetaData(_SqlMetaDataSet metaData, Boolean moreInfo)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryCloseInternal(Boolean closeReader)
   at System.Data.SqlClient.SqlDataReader.Close()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
你知道为什么我们会有紧密的联系吗


谢谢你的帮助

尝试使用?嗯,这是个好主意。我会马上调查,但奇怪的是它是随机发生的。并非此页面上的所有请求都会获得此执行。延迟加载的导航属性似乎不是问题,这些属性不会在任何地方使用,并且表函数返回类型没有任何导航属性
public class PeopleGetter : IPeopleGetter
{
    public List<object> GetResult()
    {
        using (var siteEntities = new SiteEntities())
        {
            IQueryable<someResultEntity> result = 
                 siteEntities.GetPeopleNames()
                   .SortBy("Name", "asc")
                   .Skip(20*query.CurrentPagingPosition)
                   .Take(20);


            List<People> people = SelectResultToPeople(result)
            return people;

        }
    }

    private List<People> SelectResultToPeople(IQueryable<someResultEntity> result)
    {
        return result
               .ToList()
               .Select(x => new People { })
               .ToList();
    }
}
public class PerCallContextLifeTimeManager : HierarchicalLifetimeManager
{
    private readonly string _key =  string.Format("PerCallContextOrRequestLifeTimeManager_{0}", Guid.NewGuid());

    public override object GetValue()
    {
        if (HttpContext.Current != null)
            return GetFromHttpContext();
        else
            return base.GetValue();
    }

    private object GetFromHttpContext()
    {
        return HttpContext.Current.Items[_key];
    }

    public override void SetValue(object newValue)
    {
        if (HttpContext.Current != null)
            SetInHttpContext(newValue);
        else
                    base.SetValue(newValue);
    }

    private void SetInHttpContext(object newValue)
    {
            HttpContext.Current.Items.Add(_key, newValue);
    }


    public override void RemoveValue()
    {
    }
}