C
C#8mo ago
Punica

SqlException without stopping or catching

Hello I get a Sql Exception in the following code when I connect to a MSSQL Server and make a Database. The Problem with it I can not catcvh the exception with my try block to see why that happening.
c#
private static void CreateGenericDatabase(DataBaseConfig dbBaseConfig)
{
try
{
string connectionString = DatabaseConnectionGen.GetConnectionString(dbBaseConfig.SqlServerIp, dbBaseConfig.SqlType, dbBaseConfig.Username, dbBaseConfig.Password, dbBaseConfig.WindowsAuth, "CoreDb");

using TestDbContext dbContext = DatabaseConnectionGen.GenerateBuilder(dbBaseConfig.SqlType, connectionString);
dbContext.Database.EnsureCreated();
using IDbContextTransaction transaction = dbContext.Database.BeginTransaction();
dbContext.Database.Migrate();
transaction.Commit();
}
catch(SqlException ex)
{
// Detaillierte Protokollierung der SqlException
Debug.WriteLine("SQL Exception:");
Debug.WriteLine($"Message: {ex.Message}");
Debug.WriteLine($"Error Code: {ex.ErrorCode}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
if (ex.InnerException != null)
{
Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}

throw;
}
catch (Exception ex)
{
// Detaillierte Protokollierung anderer Ausnahmen
Debug.WriteLine("Exception:");
Debug.WriteLine($"Message: {ex.Message}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
if (ex.InnerException != null)
{
Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}

throw; // Re-throw the exception to propagate it further
}
}
c#
private static void CreateGenericDatabase(DataBaseConfig dbBaseConfig)
{
try
{
string connectionString = DatabaseConnectionGen.GetConnectionString(dbBaseConfig.SqlServerIp, dbBaseConfig.SqlType, dbBaseConfig.Username, dbBaseConfig.Password, dbBaseConfig.WindowsAuth, "CoreDb");

using TestDbContext dbContext = DatabaseConnectionGen.GenerateBuilder(dbBaseConfig.SqlType, connectionString);
dbContext.Database.EnsureCreated();
using IDbContextTransaction transaction = dbContext.Database.BeginTransaction();
dbContext.Database.Migrate();
transaction.Commit();
}
catch(SqlException ex)
{
// Detaillierte Protokollierung der SqlException
Debug.WriteLine("SQL Exception:");
Debug.WriteLine($"Message: {ex.Message}");
Debug.WriteLine($"Error Code: {ex.ErrorCode}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
if (ex.InnerException != null)
{
Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}

throw;
}
catch (Exception ex)
{
// Detaillierte Protokollierung anderer Ausnahmen
Debug.WriteLine("Exception:");
Debug.WriteLine($"Message: {ex.Message}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
if (ex.InnerException != null)
{
Debug.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}

throw; // Re-throw the exception to propagate it further
}
}
The connection string is fine and evrythings work as intended but I get in the output windows the following Exception. "Microsoft.Data.SqlClient.SqlException" in Microsoft.Data.SqlClient.dll and "Microsoft.Data.SqlClient.SqlException" in Microsoft.EntityFrameworkCore.Relational.dll
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server