source/TestCommon/source/FunctionApp.TestCommon/Database/SqlServerDatabaseManager.cs
/// <summary>
/// We want to create the local SQL database for integration tests so it is similar to the Azure SQL database
/// with regards to collation and other relevant properties. EF Core currently does not support collation as
/// part of creation, so we must first create an empty database with the correct collation and then apply the
/// schema using EF Core.
///
/// Connect to master database and create a database without schema.
/// </summary>
We want to create the local SQL database for integration tests so it is similar to the Azure SQL database
with regards to collation and other relevant properties. EF Core currently does not support collation as
part of creation, so we must first create an empty database with the correct collation and then apply the
schema using EF Core.

Connect to master database and create a database without schema.
We want to create the local SQL database for integration tests so it is similar to the Azure SQL database with regards to collation and other relevant properties . EF Core currently does not support collation as part of creation so we must first create an empty database with the correct collation and then apply the schema using EF Core . Connect to master database and create a database without schema .
private void CreateLocalDatabaseWithoutSchema(TContextImplementation context)
        {
            const int dbNameAlreadyExistsExceptionNumber = 1801;
            var databaseName = GetDatabaseName(context);
            var createDatabaseCommandText = BuildCreateDatabaseCommandText(databaseName);
            var retryPolicy = Policy
                .Handle<Exception>()
                .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
            var cancellationToken = new CancellationTokenSource();
            var policyContext = new Context("RetryContext")
            {
                { "CancellationTokenSource", cancellationToken },
            };
            retryPolicy.Execute(
                (ctx, ct) =>
                {
                    using var masterDbConnection = new SqlConnection(_sqlServerConnectionStringProvider.BuildConnectionStringForDatabaseName("master"));
                    using var command = new SqlCommand(createDatabaseCommandText, masterDbConnection);
                    masterDbConnection.Open();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (SqlException sqlException)
                    {
                        if (sqlException.Number == dbNameAlreadyExistsExceptionNumber)
                        {
                            cancellationToken.Cancel();
                        }
                    }
                },
                policyContext,
                cancellationToken.Token);
        }
private void CreateLocalDatabaseWithoutSchema ( TContextImplementation context ) { const int dbNameAlreadyExistsExceptionNumber = 1801 ; var databaseName = GetDatabaseName ( context ) ; var createDatabaseCommandText = BuildCreateDatabaseCommandText ( databaseName ) ; var retryPolicy = Policy . Handle < Exception > ( ) . WaitAndRetry ( 5 , retryAttempt => TimeSpan . FromSeconds ( Math . Pow ( 2 , retryAttempt ) ) ) ; var cancellationToken = new CancellationTokenSource ( ) ; var policyContext = new Context ( " " ) { { " " , cancellationToken } , } ; retryPolicy . Execute ( ( ctx , ct ) => { using var masterDbConnection = new SqlConnection ( _sqlServerConnectionStringProvider . BuildConnectionStringForDatabaseName ( " " ) ) ; using var command = new SqlCommand ( createDatabaseCommandText , masterDbConnection ) ; masterDbConnection . Open ( ) ; try { command . ExecuteNonQuery ( ) ; } catch ( SqlException sqlException ) { if ( sqlException . Number == dbNameAlreadyExistsExceptionNumber ) { cancellationToken . Cancel ( ) ; } } } , policyContext , cancellationToken . Token ) ; }
We want to create the local SQL database for integration tests so it is similar to the Azure SQL database
with regards to collation and other relevant properties.
We want to create the local SQL database for integration tests so it is similar to the Azure SQL database with regards to collation and other relevant properties .
// Overview of all exception numbers: https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/cc645603(v=sql.105)?redirectedfrom=MSDN // Retry a specified number of times, using a function to // calculate the duration to wait between retries based on // the current retry attempt (allows for exponential backoff) // In this case will wait for //  2 ^ 1 = 2 seconds then //  2 ^ 2 = 4 seconds then //  2 ^ 3 = 8 seconds then //  2 ^ 4 = 16 seconds then //  2 ^ 5 = 32 seconds // More than 30-something connections to the master database can exhaust the connection pool, so we retry a few times. // Cancel the retry policy if database name already exists.
context
TContextImplementation