You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cosmos DB Entity Framework for .Net 9 now requires all LINQ functions to be asynchronous. When attempting to use the synchronous method, it throws the following error:
'Microsoft.EntityFrameworkCore.Database.SyncNotSupported': 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.
For example, Entity.ToList() will fail while Entity.ToListAsync() will succeed.
I have found that the following code changes to EntityFrameworkCoreXmlRepository will solve the problem. Note where I check if DB is Cosmos, and there make async calls. Someone more cleaver than I can probably simplify this.
Question, can all EF providers support asynchronous methods? If so, then maybe make this async for all and not worry if this is for Cosmos or not?
publicvirtualIReadOnlyCollection<XElement>GetAllElements(){// forces complete enumerationreturnGetAllElementsCore().ToList().AsReadOnly();IEnumerable<XElement>GetAllElementsCore(){using(varscope=_services.CreateScope()){varcontext=scope.ServiceProvider.GetRequiredService<TContext>();List<DataProtectionKey>keys;// Cosmos DB EF 9 supports only async methods.if(context.Database.IsCosmos()){keys=context.DataProtectionKeys.AsNoTracking().ToListAsync().Result;}else{keys=context.DataProtectionKeys.AsNoTracking();}foreach(varkeyinkeys){_logger.ReadingXmlFromKey(key.FriendlyName!,key.Xml);if(!string.IsNullOrEmpty(key.Xml)){yieldreturnXElement.Parse(key.Xml);}}}}}/// <inheritdoc />publicvoidStoreElement(XElementelement,stringfriendlyName){using(varscope=_services.CreateScope()){varcontext=scope.ServiceProvider.GetRequiredService<TContext>();varnewKey=newDataProtectionKey(){FriendlyName=friendlyName,Xml=element.ToString(SaveOptions.DisableFormatting)};context.DataProtectionKeys.Add(newKey);_logger.LogSavingKeyToDbContext(friendlyName,typeof(TContext).Name);// Cosmos DB EF 9 supports only async methods.if(context.Database.IsCosmos()){context.SaveChangesAsync().Wait();}else{context.SaveChanges();}}}
Expected Behavior
No response
Steps To Reproduce
Create an asp.net project, .Net 9, with a Cosmos DB database. Then add data protection using the DbContext. Here is example code that will cause the error:
// Add shared data protection herebuilder.Services.AddDataProtection().SetApplicationName("editor").UseCryptographicAlgorithms(newAuthenticatedEncryptorConfiguration{EncryptionAlgorithm=EncryptionAlgorithm.AES_256_CBC,ValidationAlgorithm=ValidationAlgorithm.HMACSHA256}).PersistKeysToDbContext<ApplicationDbContext>();
Note: The ApplicationDbContext uses Cosmos DB.
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered:
Added custom data persister so I can continue migration to .Net 9.
toiyabe
changed the title
DataProtection.PersistKeysToDbContext fails when using Cosmos DB EF 9. Possible included here.
DataProtection.PersistKeysToDbContext fails when using Cosmos DB EF 9. Possible fix included here.
Jan 9, 2025
Is there an existing issue for this?
Describe the bug
Cosmos DB Entity Framework for .Net 9 now requires all LINQ functions to be asynchronous. When attempting to use the synchronous method, it throws the following error:
'Microsoft.EntityFrameworkCore.Database.SyncNotSupported': 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.
For example, Entity.ToList() will fail while Entity.ToListAsync() will succeed.
I have found that the following code changes to
EntityFrameworkCoreXmlRepository
will solve the problem. Note where I check if DB is Cosmos, and there make async calls. Someone more cleaver than I can probably simplify this.Question, can all EF providers support asynchronous methods? If so, then maybe make this async for all and not worry if this is for Cosmos or not?
Expected Behavior
No response
Steps To Reproduce
Create an asp.net project, .Net 9, with a Cosmos DB database. Then add data protection using the DbContext. Here is example code that will cause the error:
Note: The
ApplicationDbContext
uses Cosmos DB.Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: