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

How do I get the Cosmos database Id so I can use GetCosmosClient() #25063

Closed
ghost opened this issue Jun 9, 2021 · 3 comments · Fixed by #28644
Closed

How do I get the Cosmos database Id so I can use GetCosmosClient() #25063

ghost opened this issue Jun 9, 2021 · 3 comments · Fixed by #28644
Labels
area-cosmos closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@ghost
Copy link

ghost commented Jun 9, 2021

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...

var ctx = ServiceProvider.GetRequiredService<MyCosmosDbContext>();
var client = ctx.Database.GetCosmosClient();
var databaseId = HOW_DO_I_GET_THIS;
var containerName = ctx.Model.GetDefaultContainer();
var container = client.GetContainer(databaseId, containerName);
var result = container.GetItemQueryStreamIterator("query");

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

@ghost ghost added the customer-reported label Jun 9, 2021
@ghost ghost changed the title How do I get the Cosmos Database name so I can use GetCosmosClient() How do I get the Cosmos database Id so I can use GetCosmosClient() Jun 9, 2021
@AndriySvyryd
Copy link
Member

There's currently no public way of doing it. We could add ctx.Database.GetCosmosDatabase() or ctx.Database.GetCosmosDatabaseId()

@ghost
Copy link
Author

ghost commented Jun 14, 2021

@AndriySvyryd Many thanks for your reply. ctx.Database.GetCosmosDatabase() would be simpler to use but I guess could have concurrency issues if not a new database connection. ctx.Database.GetCosmosDatabaseId() would give more flexibility/control and makes any concurrency issues the caller's responsibility so is possibly the better option.

@koenbeuk
Copy link

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);

@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Oct 27, 2021
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 9, 2022
@AndriySvyryd AndriySvyryd removed their assignment Aug 9, 2022
AndriySvyryd added a commit that referenced this issue Aug 9, 2022
Add a method to get the configured database name

Fixes #26491
Fixes #25063
AndriySvyryd added a commit that referenced this issue Aug 9, 2022
Add a method to get the configured database name

Fixes #26491
Fixes #25063
@ghost ghost closed this as completed in #28644 Aug 9, 2022
ghost pushed a commit that referenced this issue Aug 9, 2022
Add a method to get the configured database name

Fixes #26491
Fixes #25063
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc1 Aug 12, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc1, 7.0.0 Nov 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants