我有一个应用程序具有多个DbContext
共享连接字符串的子类(这是为了避免 EF 使用单个大DbContext
类型的可怕启动时间)。在某些点(对我来说是可重复的,对其他人来说有些重复),我在尝试进行数据库查询时收到以下错误:
System.Data.EntityException:“底层提供程序在打开时失败。”
内部异常:
System.Data.SqlClient.SqlException“用户'用户名'登录失败”
如果我切换回使用单个 Giant ,问题似乎就消失了DbContext
。
有谁知道这意味着什么/如何解决它?如果我重复使用相同的DbConnection
对象DbContexts
(而不是只使用相同的连接字符串)会有帮助吗?该连接字符串已经成功地在同一个请求中进行了多次查询,因此不可能是凭据错误。
我正在使用 ASP.NET MVC 3、EF 4.3.1、.NET 4.0、VS 2010。
相关的堆栈跟踪如下:
[SqlException (0x80131904): Login failed for user 'testing_net'.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +6351920
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +412
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1363
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +53
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +6366878
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +6366793
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +352
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +831
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +49
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +6368598
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +78
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +2194
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +89
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6372110
System.Data.SqlClient.SqlConnection.Open() +300
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +67
[EntityException: The underlying provider failed on Open.]
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +11108990
System.Data.EntityClient.EntityConnection.Open() +142
System.Data.Objects.ObjectContext.EnsureConnection() +97
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +57
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +47
System.Linq.Enumerable.Single(IEnumerable`1 source) +156
代码的大致轮廓如下:
// db context:
public class MyDbContext<T> : DbContext {
public MyDbContext(string connectionString) : base(connectionString) { }
protected override OnModelCreating(DbModelBuilder builder)
{
// constructs the model base on the type of T
// code first POCO entities are annotated with an attribute that links
// them to one or more types T
}
}
我使用 AutoFac 的 DI 将我的 DbContext 注入到我的数据访问层的服务中。上下文的生命周期是 HttpRequest 的长度。
实际的异常发生在对 Queryable.Single() 的调用上。
编辑:我认为这个问题可能是相关的,但我不确定如何理解所描述的竞争条件。
编辑:既然我理解了这个问题,我可以发布有问题的代码:
MyDbContext<T1> db1 = ...
var connectionString = db1.Database.Connection.ConnectionString;
var dbContext2 = new MyDbContext<T2>(connectionString);