How to update data from a model into RavenDB using C#?

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1065
Let's say we have a model as under

public class RSS
{
public string Id { get; set; }
public string QuestionID { get; set; }
public string QuestionTitle { get; set; }
public string QuestionDescription { get; set; }
public DateTime PublishDate { get; set; }
}


The below piece of code will update data from from RSS model to RavenDB

public RSS UpdateRssFeed(string id)
{
var modifiedRss = new RSS();
using (var ds = new DocumentStore
{
Url = "http://localhost:8080",
DefaultDatabase = "CRUDDemo"
}.Initialize())

using (var session = ds.OpenSession())
{
var rss = session.Load<RSS>(id); //find the record with the id

if (null != rss)
{
//update the question title and question description
rss.QuestionTitle = "This is an updated question title";
rss.QuestionDescription = "This is an updated question";

//Saves all the pending changes to the server.
session.SaveChanges();

//load the modified RSS record
modifiedRss = session.Load<RSS>(id);
}
}
return modifiedRss;
}


As a first step, we are using the DocumentStore class that inherits from the abstract class DocumentStoreBase. The DocumentStore class is manages access to RavenDB and open sessions to work with RavenDB.The DocumentStore class needs a URL and optionally the name of the database. Our RavenDB server is running at 8080 port.Also we specified a DefaultDatabase name which is CRUDDemo here. The function Initialize() initializes the current instance.From the DocumentStore we get the Session object which we will use for all the operations in RavenDB. The method OpenSession() opens the session.This Session object provides the transactional functionality and also prepares a set of modifications that are submitted to the database with the SaveChanges()method.

Comments or Responses

Login to post response