RavenDb學習(二)簡單的增刪查改
阿新 • • 發佈:2022-04-29
在上一節當中已經介紹了RavenDb的文件設計模式,這一節我們要具體講一講如何使用api去訪問RavenDb 1.連線RavenDb var documentStore = new DocumentStore { Url = "http://myravendb.mydomain.com/" }; documentStore.Initialize(); var documentStore = new DocumentStore { ConnectionStringName = "MyRavenConStr" }; 在app.config中配置如下: <connectionStrings> <add name="Local" connectionString="DataDir = ~Data"/> <add name="Server" connectionString="Url = http://localhost:8080"/> <add name="Secure" connectionString="Url = http://localhost:8080;user=beam;password=up;ResourceManagerId=d5723e19-92ad-4531-adad-8611e6e05c8a"/> </connectionStrings> 引數: DataDir - embedded mode的引數, 只能例項化一個EmbeddableDocumentStore, Url - server mode的引數 User / Password - server mode的引數 Enlist - whatever RavenDB should enlist in distributed transactions. 不適合Silverlight ResourceManagerId - 可選的, server mode的引數, the Resource Manager Id that will be used by the Distributed Transaction Coordinator (DTC) service to identify Raven. A custom resource manager id will need to be configured for each Raven server instance when Raven is hosted more than once per machine.不適合Silverlight Database - server mode的引數,不是用內建的資料庫 Url的方式: Url = http://ravendb.mydomain.com connect to a remote RavenDB instance at ravendb.mydomain.com, to the default database Url = http://ravendb.mydomain.com;Database=Northwind connect to a remote RavenDB instance at ravendb.mydomain.com, to the Northwind database there Url = http://ravendb.mydomain.com;User=user;Password=secret connect to a remote RavenDB instance at ravendb.mydomain.com, with the specified credentials DataDir = ~App_DataRavenDB;Enlist=False use embedded mode with the database located in the App_DataRavenDB folder, without DTC support. 2.Session使用案例 //寫入 string companyId; using (var session = documentStore.OpenSession()) { var entity = new Company { Name = "Company" }; session.Store(entity); session.SaveChanges(); companyId = entity.Id; } //讀取 using (var session = documentStore.OpenSession()) { var entity = session.Load<Company>(companyId); Console.WriteLine(entity.Name); } //刪除,一旦刪除無法恢復 using (var session = documentStore.OpenSession()) { session.Delete(existingBlogPost); session.SaveChanges(); } 下面兩種方式也可以刪除 session.Advanced.Defer(new DeleteCommandData { Key = "posts/1234" }); session.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null); 3.查詢 //PageSize 如果沒有設定PageSize,客戶端呼叫是一次128條記錄,服務端呼叫是一次1024條記錄,遠端呼叫是一次30條記錄,可以配置。 RavenDb為了加快查詢資料的速度,它在後臺使用的是lucene的索引方式,通過linq來生成HTTP RESTful API。 //查詢,用linq的方式查詢很方便 var results = from blog in session.Query<BlogPost>() where blog.Category == "RavenDB" select blog; var results = session.Query<BlogPost>() .Where(x => x.Comments.Length >= 10) .ToList(); //分頁查詢 var results = session.Query<BlogPost>() .Skip(20) // skip 2 pages worth of posts .Take(10) // Take posts in the page size .ToArray(); // execute the query //分頁的時候,我們一次取10條,但是我們也要知道總共有多少條資料,我們需要通過TotalResults來獲得 RavenQueryStatistics stats; var results = session.Query<BlogPost>() .Statistics(out stats) .Where(x => x.Category == "RavenDB") .Take(10) .ToArray(); var totalResults = stats.TotalResults; //跳過指定的臨時的資料集,每次查詢都記錄下上一次查詢記錄的跳過的查詢記錄,該值儲存在SkippedResults RavenQueryStatistics stats; // get the first page var results = session.Query<BlogPost>() .Statistics(out stats) .Skip(0 * 10) // retrieve results for the first page .Take(10) // page size is 10 .Where(x => x.Category == "RavenDB") .Distinct() .ToArray(); var totalResults = stats.TotalResults; var skippedResults = stats.SkippedResults; // get the second page results = session.Query<BlogPost>() .Statistics(out stats) .Skip((1 * 10) + skippedResults) // retrieve results for the second page, taking into account skipped results .Take(10) // page size is 10 .Where(x => x.Category == "RavenDB") .Distinct() .ToArray(); //查詢出來的資料不一定是最新的,如果stats.IsStale不為true的話,它就報錯的啦 if (stats.IsStale) { // Results are known to be stale } //設定獲取時間,更新時間截止到某個時刻 RavenQueryStatistics stats; var results = session.Query<Product>() .Statistics(out stats) .Where(x => x.Price > 10) .Customize(x => x.WaitForNonStaleResultsAsOf(new DateTime(2011, 5, 1, 10, 0, 0, 0))) .ToArray(); //設定查詢返回最後一次更新 documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;