Question: I get some data from a source and I want to add them in the database, so I open a context for each batch and use a transaction to commit the changes after all the data was added.

My code:

```
      using (var conn = new SqlConnection(dataAccessSettings.DatabaseConnectString))
      {
        conn.Open();

        using (var sqlTxn = conn.BeginTransaction(System.Data.IsolationLevel.Snapshot))
        {
          foreach (var dataBatch in GetData())
          {
            using (var context = new DataContext(conn, false))
            {
              context.Database.UseTransaction(sqlTxn);
              context.DataTable.AddRange(dataBatch);

              context.SaveChanges();
            }
          }
          sqlTxn.Commit();
        }
      }

```

However, when the second context calls the `context.Database.UseTransaction(sqlTxn)` I get the following error:

```
The transaction passed in must have a non-null connection. A null connection indicates the transaction has already been completed.

```

I set the `contextOwnsConnection` constructor parameter of the context to `false` so what exactly is closing my connection?

Answer: 
You can now do this with the Az Powershell module

```
Invoke-AzRestMethod
      -Path <String>
      -Method <String>
      [-Payload <String>]
      [-AsJob]
      [-DefaultProfile <IAzureContextContainer>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

```

<https://learn.microsoft.com/en-us/powershell/azure/manage-azure-resources-invoke-azrestmethod?view=azps-5.9.0>
There is no built-in powershell command equals `az rest` currently.

My workaround is to use the command below, you could simply use it to get the specific token for a specific resource with your login account/service principal, e.g. `https://management.azure.com`, `https://graph.microsoft.com`, it can also be other resources, even the `app-id` of your custom API in AAD.

**Sample**:

```
Connect-AzAccount
$resource = "https://graph.microsoft.com"
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$Token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken

```

[![enter image description here](https://i.stack.imgur.com/nA6yT.png)](https://i.stack.imgur.com/nA6yT.png)

Decode the token, we can find the `audience` is correct.

[![enter image description here](https://i.stack.imgur.com/lac1K.png)](https://i.stack.imgur.com/lac1K.png)

After getting the token, you can simply use the [`Invoke-RestMethod`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7) to call any REST API you want, for the format, you can check this [sample](https://stackoverflow.com/a/60255738/9455659).