How to check if a table exists in Azure.Data.Tables?
See original GitHub issueLibrary 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:
- Created a year ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top GitHub Comments
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:
I agree that we could add this context to the migration guide. I’ll create an issue to track this.
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.