gpt4 book ai didi

c# - 为什么我的 Entity Framework 测试失败了?

转载 作者:行者123 更新时间:2023-11-28 20:41:46 26 4
gpt4 key购买 nike

我关注了this tutorial创建用于 Entity Framework 6 的 AWS RDS SQL Server 数据库。

我在 Visual Studio 2013 AWS Explorer 中创建了一个 RDS 实例,并在 Web.config 中添加了连接字符串,如下所示:

<connectionStrings>
<add
name="SystemDBContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=dataSourceAddress,1433;Initial Catalog=instanceName;User ID=userID;Password=password"
/>
</connectionStrings>

这个检查我可以打开连接的测试失败了:

    [TestMethod]
public void TestSystemDb()
{
TestConnection();
}

private void TestConnection()
{
using (var db = new SystemDBContext())
{
var conn = db.Database.Connection;
conn.Open();
}
}
}

堆栈跟踪:

Test method BabyChangeFinder.Tests.DatabaseTest.TestSystemDb threw exception: 

System.Data.SqlClient.SqlException: Cannot open database "BabyChangeFinder.DataAccess.SystemDBContext" requested by the login. The login failed.

Login failed for user 'RIG\Nick'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, ref Boolean dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, ref DbConnectionInternal connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, ref DbConnectionInternal connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, ref DbConnectionInternal connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at BabyChangeFinder.Tests.DatabaseTest.TestConnection() in DatabaseTest.cs: line 23
at BabyChangeFinder.Tests.DatabaseTest.TestSystemDb() in DatabaseTest.cs: line 13

最佳答案

嗯,由于某种原因无法建立连接。如果您没有用 try/catch 包装 TestConnection 方法,您会看到在连接建立过程中发生的详细异常,而不是 AssertFailedException。这是一个很好的例子,说明如何不使用异常处理!只需将其删除并再次运行测试即可。

private void TestConnection()
{
using (var db = new SystemDBContext())
{
var conn = db.Database.Connection;
conn.Open();
}
}

编辑:您已经在该异常处理程序中记下异常的堆栈跟踪。您也可以尝试写下 e.Message,因为这是您肯定希望看到的异常文本,但是当测试失败时,此消息也应该在测试结果中可见...

关于c# - 为什么我的 Entity Framework 测试失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29727383/

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