gpt4 book ai didi

sql-server-2008 - "The underlying provider failed on Open" Entity Framework 错误

转载 作者:行者123 更新时间:2023-12-04 07:10:45 27 4
gpt4 key购买 nike

我一直收到错误消息“基础提供程序在打开时失败。”最近,我一直在努力找出原因。仔细检查错误后,内部异常是“连接未关闭。连接的当前状态为连接。”堆栈跟踪是

"at System.Data.ProviderBase.DbConnectionClosedConnecting.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)"

据我所知,错误似乎是随机发生的,或者可能是在对应用程序进行更改时发生的。任何帮助将不胜感激。

我目前的设置是

  • IIS Express
  • Microsoft SQL Server 速成版
  • Visual Studio 2012
  • MVC Web API\Asp.net MVC 4
  • Entity Framework 5

我的数据层是根据存储库模式建模的,并通过结构图注入(inject)。

存储库

public class Repository<T> : IDisposable, IRepository<T> where T : class
{
private readonly DbSet<T> _dbSet;
private readonly DbContext _dbContext;

public Repository(IDbContextFactory<MyContext> dbContextFactory)
{
_dbContext = dbContextFactory.Create();
_dbSet = _dbContext.Set<T>();
}

//removed for brevitity

public void Dispose()
{
_dbContext.Dispose();
}

DbContextFactory

public class MyContextFactor : IDbContextFactory<MyContext>, IDisposable
{
private MyContext _dataContext;

public MyContextFactor Create()
{
return _dataContext ?? (_dataContext = new MyContext());
}

public void Dispose()
{
_dataContext.Dispose();
}
}

IOC 结构图注册

For(typeof(IDbContextFactory<MyContext>)).HybridHttpOrThreadLocalScoped().Use(typeof(MyContextFactory));

连接字符串

<add name="MyContextConnectionString" connectionString="metadata=res://*/Model.MyProject.csdl|res://*/Model.MyProject.ssdl|res://*/Model.MyProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=MyDB;User Id=SQLUser;Password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>

我已经尝试解决这个问题,但到目前为止还没有奏效

  • 通过结构映射范围确保上下文是每个 http 上下文
  • 添加处理到存储库
  • 添加处理到 ContextFactory
  • 从 Intergrated Sql auth 切换到 Sql authentication

最佳答案

这原来是一个被缓存的 ActionFilterAttribute。 ActionFilterAttribute 有一个 Entity Framework 上下文,它没有被正确处理,而是在请求之间共享。

关于sql-server-2008 - "The underlying provider failed on Open" Entity Framework 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13168166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com