Repository: dotnet/efcore
File: src/Microsoft.Data.Sqlite.Core/SqliteConnectionInternal.cs (C#)

Code before:
            }
            else if (!Path.IsPathRooted(filename))
            {
                filename = Path.Combine(dataDirectory, filename);
            }
        }

        var vfs = !string.IsNullOrWhiteSpace(connectionOptions.Vfs)
            ? connectionOptions.Vfs
            : null;
        var rc = sqlite3_open_v2(filename, out _db, flags, vfs: vfs);
        try
        {
            SqliteException.ThrowExceptionForRC(rc, _db);

            if (connectionOptions.Password.Length != 0)
            {
                if (SQLitePCLExtensions.EncryptionSupported(out var libraryName) == false)
                {
                    throw new InvalidOperationException(Resources.EncryptionNotSupported(libraryName));
                }

                // NB: SQLite doesn't support parameters in PRAGMA statements, so we escape the value using the
                //     quote function before concatenating.
                var quotedPassword = string.Empty;
                using (var inMemoryConnection = new SqliteConnection("Filename=:memory:"))
                {
                    inMemoryConnection.Open();
                    quotedPassword = ExecuteScalar(
                        "SELECT quote($password);",
                    connectionOptions.Password,
                    connectionOptions.DefaultTimeout);
                }
                ExecuteNonQuery(
                    "PRAGMA key = " + quotedPassword + ";",
                    connectionOptions.DefaultTimeout);

                if (SQLitePCLExtensions.EncryptionSupported() != false)
                {
                    // NB: Forces decryption. Throws when the key is incorrect.
                    ExecuteNonQuery(
                        "SELECT COUNT(*) FROM sqlite_master;",
                        connectionOptions.DefaultTimeout);
                }
            }

            if (connectionOptions.ForeignKeys.HasValue)
            {
                ExecuteNonQuery(
                    "PRAGMA foreign_keys = " + (connectionOptions.ForeignKeys.Value ? "1" : "0") + ";",
                    connectionOptions.DefaultTimeout);

Review comment (suggestion):
I wouldn't use the ADO.NET interfaces (e.g. SqliteConnection) here. Use the lower-level SQLitePCLRaw APIs (e.g. sqlite3_open_v2) instead.

Code after:
            }
            else if (!Path.IsPathRooted(filename))
            {
                filename = Path.Combine(dataDirectory, filename);
            }
        }

        var vfs = !string.IsNullOrWhiteSpace(connectionOptions.Vfs)
            ? connectionOptions.Vfs
            : null;
        var rc = sqlite3_open_v2(filename, out _db, flags, vfs: vfs);
        try
        {
            SqliteException.ThrowExceptionForRC(rc, _db);

            if (connectionOptions.Password.Length != 0)
            {
                if (SQLitePCLExtensions.EncryptionSupported(out var libraryName) == false)
                {
                    throw new InvalidOperationException(Resources.EncryptionNotSupported(libraryName));
                }

                // NB: SQLite doesn't support parameters in PRAGMA statements, so we escape the value using the
                //     quote function before concatenating.
                var quotedPassword = QuotePassword(connectionOptions.Password);
                ExecuteNonQuery(
                    "PRAGMA key = " + quotedPassword + ";",
                    connectionOptions.DefaultTimeout);

                if (SQLitePCLExtensions.EncryptionSupported() != false)
                {
                    // NB: Forces decryption. Throws when the key is incorrect.
                    ExecuteNonQuery(
                        "SELECT COUNT(*) FROM sqlite_master;",
                        connectionOptions.DefaultTimeout);
                }
            }

            if (connectionOptions.ForeignKeys.HasValue)
            {
                ExecuteNonQuery(
                    "PRAGMA foreign_keys = " + (connectionOptions.ForeignKeys.Value ? "1" : "0") + ";",
                    connectionOptions.DefaultTimeout);
            }

            if (connectionOptions.RecursiveTriggers)
            {
                ExecuteNonQuery(
                    "PRAGMA recursive_triggers = 1;",
                    connectionOptions.DefaultTimeout);
            }