Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Cosmos DB Preview Emulator with EF Core and Enrich API fails writing to the database #8177

Open
1 task done
christiannagel opened this issue Mar 19, 2025 · 6 comments
Labels
area-integrations Issues pertaining to Aspire Integrations packages azure Issues associated specifically with scenarios tied to using Azure azure-cosmosdb Issues related to Azure CosmosDB

Comments

@christiannagel
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I've issues running my Codebreaker solution using the Azure Cosmos DB preview emulator with the Enrich API using EF Core - it's running with the local installed emulator, and in Azure. With the preview emulator it fails.
Now I tried using @davidfowl project https://github.com/davidfowl/aspire-ai-chat-demo. With the code as it is, everything runs successfully.
Changing it to use the .NET Aspire Enrich API, it fails.

Expected Behavior

When changing ChatApi/Program.cs from

builder.AddCosmosDbContext<AppDbContext>("conversations", "db");

https://github.com/davidfowl/aspire-ai-chat-demo/blob/6296a504010e77fce550c8f2aba893ed0e4f34cf/ChatApi/Program.cs#L7

to

builder.Services.AddCosmos<AppDbContext>("conversations", "db");
builder.EnrichCosmosDbContext<AppDbContext>();

https://github.com/christiannagel/aspire-ai-chat-demo/blob/192969f0eb254b39639849b005f90db4ac5dcc84/ChatApi/Program.cs#L8-L9

No data is written to the database. I saw a DbUpdateException.

Steps To Reproduce

No response

Exceptions (if any)

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the item with id '0195aee7-e4da-7a43-be30-78fcc1faaecb'. See the inner exception for details.
 ---> System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
   at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
   at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms, Boolean useOdbcRules)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at Microsoft.Azure.Cosmos.CosmosClientOptions.GetValueFromConnectionString[T](String connectionString, String keyName, T defaultValue)
   at Microsoft.Azure.Cosmos.CosmosClientOptions.GetAccountEndpoint(String connectionString)
   at Microsoft.Azure.Cosmos.CosmosClient..ctor(String connectionString, CosmosClientOptions clientOptions)
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.SingletonCosmosClientWrapper.get_Client()
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.get_Client()
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.CreateItemOnceAsync(DbContext _, ValueTuple`4 parameters, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.CreateItemOnceAsync(DbContext _, ValueTuple`4 parameters, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.CreateItemOnceAsync(DbContext _, ValueTuple`4 parameters, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosDatabaseWrapper.SaveChangesAsync(IList`1 entries, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosDatabaseWrapper.SaveChangesAsync(IList`1 entries, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_0`2.<<ExecuteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at ChatExtensions.<>c.<<MapChatApi>b__0_3>d.MoveNext() in D:\christian\aspire-ai-chat-demo\ChatApi\ChatApi.cs:line 46
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteTaskResult[T](Task`1 task, HttpContext httpContext)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass101_2.<<HandleRequestBodyAndCompileRequestDelegateForJson>b__2>d.MoveNext()

I don't know how this relates to changing the configuration to the Enrich API.

.NET Version info

No response

Anything else?

No response

@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Mar 19, 2025
@davidfowl davidfowl added azure-cosmosdb Issues related to Azure CosmosDB azure Issues associated specifically with scenarios tied to using Azure labels Mar 23, 2025
@davidfowl
Copy link
Member

cc @sebastienros can you take a look?

@davidfowl
Copy link
Member

@roji any ideas?

@sebastienros
Copy link
Member

@christiannagel I think you need to set the connection string

var connectionString = builder.Configuration.GetConnectionString("mydb");
builder.Services.AddDbContextPool<MyDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseCosmos(connectionString, "mydb"));
builder.EnrichCosmosDbContext<MyDbContext>();

@davidfowl
Copy link
Member

@IEvangelist

@roji
Copy link
Member

roji commented Mar 29, 2025

The usual way to configure Cosmos is actually without a connection string - there are some UseCosmos() which simply accept the required endpoint information, etc., see these getting started docs. There's indeed also a UseCosmos() accepting a connection string, but I'd recommend the other, more strongly-typed overloads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages azure Issues associated specifically with scenarios tied to using Azure azure-cosmosdb Issues related to Azure CosmosDB
Projects
None yet
Development

No branches or pull requests

4 participants