-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How do I get the Cosmos database Id so I can use GetCosmosClient() #25063
Comments
There's currently no public way of doing it. We could add |
@AndriySvyryd Many thanks for your reply. |
As a workaround, I've been using the following methods as part of my DbContext: public string GetCosmosDbName()
{
#pragma warning disable EF1001 // Internal EF Core API usage.
var dbContextOptions = this.GetService<IDbContextOptions>();
var cosmosOptionsExtension = dbContextOptions.FindExtension<CosmosOptionsExtension>();
Debug.Assert(cosmosOptionsExtension is not null);
return cosmosOptionsExtension.DatabaseName;
#pragma warning restore EF1001 // Internal EF Core API usage.
}
public string? GetCosmosContainerName(Type type)
{
var entityType = Model.FindEntityType(type);
return entityType?.GetContainer();
}
var cosmosClient = Database.GetCosmosClient();
var databaseName = GetCosmosDbName();
var databaseClient = cosmosClient.GetDatabase(databaseName);
var containerName = GetCosmosContainerName(typeof(MyEntity));
var containerClient = databaseClient.GetContainer(containerName); |
Question
Occasionally EF doesn't support what I need so I want to bypass it and make a quick cosmos query. I thought I could do this easily by using the
DbContext.GetCosmosClient()
extension method. However, I've run into the issue that I still need to know the database id and container id in order to get a container to query on.I want to do something like this...
Of course, somewhere I must call
DbContextOptionsBuilder.UseCosmos(connectionString, databaseId)
but this is different in different environments/applications so I don't have a reliable way to get the database Id that way.Any help much appreciated.
Provider and version information
Microsoft.EntityFrameworkCore 5.0.6
Microsoft.EntityFrameworkCore.Cosmos 5.0.6
Microsoft.Azure.Cosmos 3.12.0
The text was updated successfully, but these errors were encountered: