dotnet/efcore · error · InvalidOperationException

Azure Cosmos DB does not support synchronous I/O. Make sure…

Error message

Azure Cosmos DB does not support synchronous I/O. Make sure to use and correctly await only async methods when using Entity Framework Core to access Azure Cosmos DB.

What it means

EnsureCreated() is the synchronous database-creation entry point on CosmosDatabaseCreator. Because the Azure Cosmos DB SDK only exposes async APIs, the provider deliberately throws InvalidOperationException for all synchronous store-management methods. You must call the async equivalent.

Solutions

  1. Call await dbContext.Database.EnsureCreatedAsync(cancellationToken) instead.
  2. If blocked by a non-async caller, make the caller async (e.g. public static async Task Main).
  3. For tests, use the async overloads provided by the test fixture.

Example fix

// before
dbContext.Database.EnsureCreated();

// after
await dbContext.Database.EnsureCreatedAsync();
Defensive patterns

Strategy: validation

Validate before calling

// Reject sync creation on Cosmos at the call site
if (dbContext.Database.ProviderName == "Microsoft.EntityFrameworkCore.Cosmos")
{
    throw new InvalidOperationException("Use EnsureCreatedAsync for Cosmos.");
}
dbContext.Database.EnsureCreated();

Prevention

When it happens

Trigger: Calling dbContext.Database.EnsureCreated() (sync) on a Cosmos provider. Triggered by scaffolding code, synchronous startup paths, or test fixtures that use the sync API.

Common situations: Porting a relational app where EnsureCreated() worked. A synchronous Main or a non-async test setup calling EnsureCreated(). Result.Wait()/.GetAwaiter().GetResult() wrappers around the async call hit this through a different overload.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/83ee1cd105a1905b. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs:308

    /// <returns>The names of the partition key property.</returns>
    private static IReadOnlyList<string> GetPartitionKeyStoreNames(IEntityType entityType)
    {
        var properties = entityType.GetPartitionKeyProperties();
        return properties.Any()
            ? properties.Select(p => p.GetJsonPropertyName()).ToList()
            : [CosmosClientWrapper.DefaultPartitionKey];
    }

    #region Unsupported sync methods

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool EnsureCreated()
        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool EnsureDeleted()
        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool CanConnect()
        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);

View on GitHub (pinned to 3a2006ef56)