瀏覽代碼

调整查询CollectionID

CrazyIter 5 年之前
父節點
當前提交
616e115e4b
共有 1 個文件被更改,包括 27 次插入27 次删除
  1. 27 27
      TEAMModelOS.SDK/Module/AzureCosmosDB/Implements/AzureCosmosDBRepository.cs

+ 27 - 27
TEAMModelOS.SDK/Module/AzureCosmosDB/Implements/AzureCosmosDBRepository.cs

@@ -228,9 +228,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             try
             {
                 Type t = typeof(T);
-                await InitializeCollection<T>();
+              DocumentCollection collection=  await InitializeCollection<T>();
                 ResourceResponse<Document> doc =
-                    await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
+                    await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), entity);
                 //Console.WriteLine(doc.ActivityId);
                 return entity;
             }
@@ -243,19 +243,19 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         public async Task<T> Update<T>(T entity)
         {
             Type t = typeof(T);
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             ResourceResponse<Document> doc =
-                await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, t.Name), entity);
+                await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), entity);
             return entity;
         }
         public async Task<string> ReplaceObject<T>(T entity, string key)
         {
             Type t = typeof(T);
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             try
             {
                 ResourceResponse<Document> doc =
-                await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key), entity);
+                await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database,collection.Id, key), entity);
                 return key;
             }
             catch (Exception e)
@@ -270,11 +270,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         public async Task<string> ReplaceObject<T>(T entity, string key, string partitionKey)
         {
             Type t = typeof(T);
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             try
             {
                 ResourceResponse<Document> doc =
-                await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, key),
+                await CosmosClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(Database,collection.Id, key),
                 entity,
                 new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
                 return key;
@@ -293,10 +293,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             Boolean open = true;
             List<T> objs = new List<T>();
 
-            //await InitializeCollection<T>();
+            DocumentCollection collection =  await InitializeCollection<T>();
             //查询条数 -1是全部
             FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions).AsDocumentQuery();
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database,collection.Id), queryOptions).AsDocumentQuery();
             while (query.HasMoreResults)
             {
                 foreach (T obj in await query.ExecuteNextAsync())
@@ -314,10 +314,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             Type t = typeof(T);
 
             List<T> objs = new List<T>();
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             //查询条数 -1是全部
             FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions);
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), queryOptions);
 
             //  query.Where();
             return objs;
@@ -329,8 +329,8 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         {
             Type t = typeof(T);
             //List<T> objs = new List<T>();
-            await InitializeCollection<T>();
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql);
+            DocumentCollection collection =  await InitializeCollection<T>();
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database,collection.Id), sql);
             //foreach (var item in query)
             //{
             //    objs.Add(item);
@@ -344,10 +344,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             Type t = typeof(T);
             //List<T> objs = new List<T>();
             // Boolean open = IsPk;
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             //查询条数 -1是全部
             FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = IsPk };
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql, queryOptions);
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), sql, queryOptions);
             //foreach (var item in query)
             //{
             //    objs.Add(item);
@@ -360,7 +360,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
 
         public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict)
         {
-            //await InitializeCollection<T>();
+            DocumentCollection collection =  await InitializeCollection<T>();
             Type t = typeof(T);
             Boolean open = true;
             List<Filter> filters = new List<Filter>();
@@ -397,7 +397,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             await InitializeCollection<T>();
             //查询条数 -1是全部
             FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), queryOptions);
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), queryOptions);
             List<T> list = DynamicLinq.GenerateFilter<T>(query, filters).ToList();
             return list;
             //return CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name),sql);
@@ -406,15 +406,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         public async Task<string> DeleteAsync<T>(string id)
         {
             Type t = typeof(T);
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             ResourceResponse<Document> doc =
-                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id));
+                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database,collection.Id, id));
             //Console.WriteLine(doc.ActivityId);
             return id;
         }
         public async Task<T> DeleteAsync<T>(T entity)
         {
-            await InitializeCollection<T>();
+            DocumentCollection collection =  await InitializeCollection<T>();
             Type t = typeof(T);
             string PartitionKey = GetPartitionKey<T>();
             if (!string.IsNullOrEmpty(PartitionKey))
@@ -422,13 +422,13 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
                 string pkValue = entity.GetType().GetProperty(PartitionKey).GetValue(entity).ToString();
                 string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
                 ResourceResponse<Document> doc =
-              await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue), new RequestOptions { PartitionKey = new PartitionKey(pkValue) });
+              await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, collection.Id, idValue), new RequestOptions { PartitionKey = new PartitionKey(pkValue) });
             }
             else
             {
                 string idValue = entity.GetType().GetProperty("id").GetValue(entity).ToString();
                 ResourceResponse<Document> doc =
-                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, idValue));
+                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, collection.Id, idValue));
             }
 
 
@@ -440,9 +440,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         {
             Type t = typeof(T);
 
-            await InitializeCollection<T>();
+            DocumentCollection collection = await InitializeCollection<T>();
             ResourceResponse<Document> doc =
-                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, t.Name, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
+                await CosmosClient.DeleteDocumentAsync(UriFactory.CreateDocumentUri(Database, collection.Id, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
             //Console.WriteLine(doc.ActivityId);
             return id;
 
@@ -643,7 +643,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
         {
             Type t = typeof(T);
             // List<T> objs = new List<T>();
-            await InitializeCollection<T>();
+         DocumentCollection collection=   await InitializeCollection<T>();
             StringBuilder sql = new StringBuilder("select * from c where 1=1 ");
             if (dict != null)
             {
@@ -654,7 +654,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             }
             //查询条数 -1是全部
             FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = IsPk };
-            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql.ToString(), queryOptions);
+            var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, collection.Id), sql.ToString(), queryOptions);
             //foreach (var item in query)
             //{
             //    objs.Add(item);