RavenDb学习(二)简单的增删查改
在上一节当中已经介绍了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_Data\RavenDB;Enlist=False use embedded mode with the database located in the App_Data\RavenDB 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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。