question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to check if a table exists in Azure.Data.Tables?

See original GitHub issue

Library name and version

Azure.Data.Tables @ 12.5.0

Query/Question

So I’m working my way through the migration guide like a good person…

My Microsoft.Azure.Cosmos.Table reporting code (reads, not writes) absolutely requires that I check to see if the table in question exists or not, and makes decisions on that. I do not want to blindly create-table-if-not-exist.

However, I can’t see the equivalent of this method https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.table.cloudtable.exists?view=azure-dotnet in Azure.Data.Tables.

How to do it?

If the response is: “you can’t”, then I will have to create a ticket for this to be added, because I can’t migrate my code to your new package without it.

[Note: create-table-if-not-exist is obviously a good thing, but only for my data-writing code, and not for my data-reading code.]

Environment

.NET 6 Windows 10 Visual Studio 2022

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christothescommented, Apr 26, 2022

Thanks for the context. The omission of a dedicated Exists method was an intentional omission. The main reason is that in encourages racy code. For example, the result of Exists is only valid at the instant in time it was checked. If code wants to check for existence before attempting to manipulate data in the table, it should instead just attempt to manipulate the data and handle failures for when the table does not exist.

That said, it is possible to check if a table exists in a less direct way, using the TableServiceClient.Query method. You can find a sample of querying for tables here.

For this specific scenario, it might look like the following:

bool exists = false;
await foreach(var tbl in service.QueryAsync(t => t.Name == "mytable"))
{
    exists = true;
}

I agree that we could add this context to the migration guide. I’ll create an issue to track this.

0reactions
Bellarmine-Headcommented, Apr 26, 2022

Thanks @christothes

I understand the reasons for the omission. That’s fine, just as long as it’s called out in the migration guide… which you will now instigate. Thx.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check to see if a table exists in Azure.Data. ...
You can use TableServiceClient.Query method. a simple method to check to see if a table exists or not; it then make decisions based...
Read more >
TableClient.CreateIfNotExists(CancellationToken) Method
Creates a table with the name used to construct this client instance if it does not already exist. C# Copy. public virtual Azure.Response<Azure.Data.Tables....
Read more >
CloudTable.ExistsAsync Method
Initiates an asynchronous operation to determine whether a table exists. ExistsAsync(TableRequestOptions, OperationContext, CancellationToken).
Read more >
Table exists but the query cannot find it
Hi, I have at least two instances where I receive data in Log Analytics (OfficeActivity from Office 365 via the Azure Sentinel connector)...
Read more >
fast way to see if a table exists
I am querying a list of workspace ids... some of those workspaces have the ServiceMap tables, and some don't. search * | where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found