SDK を使用して整合性モデルを変更する
ItemRequestOptions クラスには、特定の要求を変更するための構成プロパティが含まれています。 このクラスを使用すると、現在の既定の整合性レベルを弱くすることができます。
たとえば、次に示す新しい変数 (名前付きオプション) には 、 最も弱い整合性レベルに構成された ConsistencyLevel プロパティが含まれています。
ItemRequestOptions options = new()
{
ConsistencyLevel = ConsistencyLevel.Eventual
};
これで、options 変数を任意の操作要求に追加できます。 この例では、コンテナーから項目を読み取る要求が行われます。 ReadItemAsync メソッドには、オプション変数を受け入れる追加のパラメーターがあります。
string id = "706cd7c6-db8b-41f9-aea2-0e0c7e8eb009";
string categoryId = "9603ca6c-9e28-4a02-9194-51cdb7fea816";
PartitionKey partitionKey = new (categoryId);
Product item = await container.ReadItemAsync<Product>(id, partitionKey, requestOptions: options);
注意
整合性レベルは、要求ごとに緩和のみでき、強化することはできません。
代わりに、CosmosClientOptions クラスを使用して、CosmosClient インスタンス全体の一貫性を緩和することもできます。
CosmosClientOptions options = new()
{
ConsistencyLevel = ConsistencyLevel.Eventual
};
CosmosClient client = new (endpoint, key, options);