{"id":1706,"date":"2011-06-23T21:15:05","date_gmt":"2011-06-23T20:15:05","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1706"},"modified":"2021-07-03T13:32:58","modified_gmt":"2021-07-03T12:32:58","slug":"first-steps-with-reavendb","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2011\/06\/first-steps-with-reavendb\/","title":{"rendered":"First Steps with RavenDB"},"content":{"rendered":"<p>Ok, I&#8217;m starting another blog post series about a database. This time it is <a href=\"http:\/\/ravendb.net\/\">RavenDB<\/a>, a document database. RavenDB is one of my favorite databases. In my opinion RavenDB has a very compelling combination of features. It stores JSON documents, supports transactions, it has very interesting query\/indexing strategy (more in a later post), a well designed client-site API and good tooling. The only big drawback and yet a strength is that RavenDB was specifically designed for the .NET platform.<\/p>\n<div id=\"attachment_1708\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-introduction.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1708\" class=\"size-medium wp-image-1708\" title=\"ravendb-introduction\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-introduction-300x189.png\" alt=\"Introducing: RavenDB\" width=\"300\" height=\"189\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-introduction-300x189.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-introduction.png 700w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1708\" class=\"wp-caption-text\">Introducing: RavenDB&nbsp;<\/p><\/div>\n<h2>Downloading \/ Installing Raven DB<\/h2>\n<p>You can download <a href=\"http:\/\/ravendb.net\/download\">RavenDB here<\/a>. It\u2019s just a zip-file which you can unzip at any location. After that you can just run the \u2018Start.bat\u2019-file. It will start up RavenDB and open the administration console in the browser. That\u2019s it, you have a RavenDB server running.<\/p>\n<h2>Adding Raven to you Solution<\/h2>\n<p>The next step is to add the client API to your solution. You either can reference the assemblies which are shipped with the RavenDB download. The \u2018Client\u2019-folder contains the assemblies for .NET 4.0, the \u2018Client 3.5\u2019-folder for .NET 3.5 and the \u2018Silverlight\u2019 folder Silverlight 4.0. Even better you can use NuGet and grab the \u2018RavenDB\u2019 package.<\/p>\n<h2>Storing Documents<\/h2>\n<p>First let\u2019s create a class which we want to store. Like this person class:<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=Person.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">class Person\n{\n\tpublic Person(string name, string sirName)\n\t{\n\t\tName = name;\n\t\tSirName = sirName;\n\t}\n\n\tpublic string Name { get; set; }\n\tpublic string SirName { get; set; }\n}<\/code><\/pre><\/noscript>\n<p>Now let\u2019s use RavenDB to persist data. First we create a document-store instance. This document-store allows us to create sessions for doing work. Each session is basically a unit of work on the database side.<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=DoStuff.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var documentStore = new DocumentStore(){Url = &quot;http:\/\/localhost:8080&quot;}.Initialize())\n{\n\tusing (var session = documentStore.OpenSession())\n\t{\n\t\t\/\/ do stuff\n\t}\n}<\/code><\/pre><\/noscript>\n<p>So let\u2019s store our first document. Call the Store()-method and pass it the person object. After that we call .SaveChanges() to commit our changes.<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=StoreYourFirstDocument.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var documentStore = new DocumentStore(){Url = &quot;http:\/\/localhost:8080&quot;}.Initialize())\n{\n\tusing (var session = documentStore.OpenSession())\n\t{\n\t\tsession.Store(new Person(&quot;Roman&quot;, &quot;Stoffel&quot;));\n\t\tsession.SaveChanges();\n\t}\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1710\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-store.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1710\" class=\"size-medium wp-image-1710\" title=\"ravendb-store\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-store-300x207.png\" alt=\"Store a Document\" width=\"300\" height=\"207\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-store-300x207.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-store.png 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1710\" class=\"wp-caption-text\">Store a Document<\/p><\/div>\n<h2>Query Documents<\/h2>\n<p>Queries (and Indexes) are written in LINQ in RavenDB. Queries and Indexes work quite different in RavenDB than in most databases. However this difference will be the topic for another blog post. For now we just query for our stored Person document.<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=QueryYourDB.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var session = documentStore.OpenSession())\n{\n\n\tvar persons = from p in session.Query&lt;Person&gt;()\n\t\t\t\t  where p.SirName == &quot;Stoffel&quot;\n\t\t\t\t  select p;\n\tforeach (var person in persons)\n\t{\n\t\tConsole.Out.WriteLine(&quot;Hi, here&#039;s {0} {1}&quot;,person.FirstName,person.SirName);\n\t}\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1711\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-query.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1711\" class=\"size-medium wp-image-1711\" title=\"ravendb-query\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-query-300x142.png\" alt=\"Query Documents\" width=\"300\" height=\"142\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-query-300x142.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-query.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1711\" class=\"wp-caption-text\">Query Documents<\/p><\/div>\n<h2>Update Documents<\/h2>\n<p>Updating documents is also easy. Query for the document, change the data you want and call .SaveChanges() to commit the changes.<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=UpdateDocument.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var session = documentStore.OpenSession())\n{\n\n\tvar person = (from p in session.Query&lt;Person&gt;()\n\t\t\t\t  where p.SirName == &quot;Stoffel&quot; \n\t\t\t\t  select p).First();\n\tperson.FirstName = &quot;More awesome Roman&quot;;\n\tsession.SaveChanges();\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1712\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-update.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1712\" class=\"size-medium wp-image-1712\" title=\"ravendb-update\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-update-300x226.png\" alt=\"Update Documents\" width=\"300\" height=\"226\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-update-300x226.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-update.png 900w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1712\" class=\"wp-caption-text\">Update Documents<\/p><\/div>\n<h2>Delete Documents<\/h2>\n<p>Of course we also can delete documents. First call the .Delete() method and pass it the document to delete and then commit the changes with .SaveChanges().<\/p>\n<script src=\"https:\/\/gist.github.com\/1043439.js?file=DeleteDocument.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">using (var session = documentStore.OpenSession())\n{\n\n\tvar person = (from p in session.Query&lt;Person&gt;()\n\t\t\t\t  where p.SirName == &quot;Stoffel&quot; \n\t\t\t\t  select p).First();\n\tsession.Delete(person);\n\tsession.SaveChanges();\n}<\/code><\/pre><\/noscript>\n<div id=\"attachment_1713\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-delete.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1713\" class=\"size-medium wp-image-1713\" title=\"ravendb-delete\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-delete-300x164.png\" alt=\"Delete Documents\" width=\"300\" height=\"164\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-delete-300x164.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/06\/ravendb-delete.png 900w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1713\" class=\"wp-caption-text\">Delete Documents<\/p><\/div>\n<h2>Documents? Tell Me More.<\/h2>\n<p>So far we stored person-objects \/ documents, ran a query and updated a person. At this stage is nearly looks like RavenDB is an object-database (like <a href=\"https:\/\/www.gamlor.info\/wordpress\/2009\/09\/db4o-the-basics\/\">db4o<\/a>). That\u2019s why next time I will go into the concepts of RavenDB. Stay tuned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok, I&#8217;m starting another blog post series about a database. This time it is RavenDB, a document database. RavenDB is one of my favorite databases. In my opinion RavenDB has&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[231],"tags":[21,296],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1706"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1706"}],"version-history":[{"count":13,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1706\/revisions"}],"predecessor-version":[{"id":3893,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1706\/revisions\/3893"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}