我正在讨论教程中的代码,如下所示:
DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
在我看来,如果您的应用程序启动并且找不到数据库(该数据库应该在应用程序重新启动/推出之间保持不变),则需要担心更大的问题,并且 CreateIfNotExists 毫无意义。
这是否是生产中推荐的模式?
最佳答案
It seems to me that if your app starts and it can't find a database (which should be persistent between app restarts/rollouts), there are bigger problems to worry about and the CreateIfNotExists is kind of meaningless.
Is this anyway a recommended pattern in production?
我认为这取决于您的需求。一般来说,使用CosmosClient.CreateDatabaseIfNotExistsAsync
可以避免麻烦。但如果您需要检查,可以使用其他方法,例如 CosmosClient.CreateDatabaseAsync
和 CosmosClient.GetDatabase(String)
。 CosmosClient.CreateDatabaseIfNotExistsAsync
x 基本上是两者的组合。设计一些逻辑,如果不存在可以得到一些提示。
关于azure - 为什么在生产中使用 CreateDatabaseIfNotExistsAsync 和其他类似的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65402048/