.Net Core

Using NoSQL Database with Dotnet Core Example | C# | LiteDB | Tutorial

Google+ Pinterest LinkedIn Tumblr

One of the very first choices to make when start developing an application is whether to use a SQL or NoSQL Database. Organizing data is a very hard task. When we say organise, we are actually categorising our data according to its type.

NoSQL systems don’t provide the level of data consistency as SQL databases. The fundamental difference between SQL and NoSQL is how data is stored and retrieved.

A conventional database like MySQL, Microsoft SQL Server or Oracle Database uses a schema.

Schema-less or NoSQL data storage is useful in the following scenarios:

  • Small Local/Desktop Applications
  • Small Web Applications
  • One database per user/account data store
  • Few parallel write operations
  • Application file format

Types of NoSQL Db

There are four common types of NoSQL technologies are available:

  1. Document databases 
  2. Wide column stores 
  3. Key-value stores 
  4. Graph databases 

In this Article, we’re going to setup & use a document database LiteDB, which is an open source MongoDB-like database with zero configuration.

LiteDB Features

  • Similar to MongoDB
  • Link Supported
  • Document Store
  • Thread & Process safe
  • Pure C# Code
  • Data recovery after write operation failure
  • Single Data file like SQLite
  • DES(AES) cryptography supported for data file
  • Store files & stream data
  • Index document fields for fast search
  • Open Source & also available at NuGet
  • Shell command line

Creating a Project

I’m going to use the VS Code for creating a project to store & retrieve Data from a NoSQL Database.

Let’s start by creating a console application using dotnet cli

dotnet new console

Installing LiteDB

LiteDB is available at NuGet. you can download from NuGet package manager if using VS or

run this command to install the library from NuGet

using package manager

Install-Package LiteDB

or using .Net CLI

dotnet add package LiteDB

Creating Model

Let’s create a simple User Model for creating User collection & inserting data.

So, create a new File as “User.cs” at the root of your Project & put the below Code.

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

Now add the below Code in your “Program.cs” File.

using System;
using System.Linq;
using LiteDB;

class Program
{
    static void Main(string[] args)
    {
        // Open database (or create if not exits)
        using(var db = new LiteDatabase(@"MyData.db"))
        {
            // Get user collection
            var users = db.GetCollection<User>("users");

            // Create your new user instance
            var user = new User
            { 
                Name = "Shehryar Khan", 
                Email = "[email protected]"
            };

            // Insert new user document (Id will be auto-incremented)
            users.Insert(user);

            var result = users.Find(x => x.Email == "[email protected]").FirstOrDefault();
            Console.WriteLine(result.Name);

            // Update a document inside a collection
            user.Name = "S Khan";
            users.Update(user);

            // Index document using a document property
            users.EnsureIndex(x => x.Name);
        }
    }
}

In the above Code, first of all we have created a Database as “MyData.db” then a User Collection.

We’re using Linq to retrieve data from DB & also Updating a record in our NoSQL DB.

Now Run your Project, you’ll see Name (Shehryar Khan) in your Console.

You can also download & run this example from Github.

View Database

There are some tools available to View your LiteDB NoSQL Database Records.

So, Download this LiteDB Manager Tool, unrar the File & run the LiteDBManager.exe

From the File Menu, click on Open Database & choose your “MyData.db” File from your Project Root Directory.

It will look like this

You can see your records are stored in the Database.

This is a very basic operation using NoSQL database, you can find some more examples at GitHub.

Comment if you find any difficulty, I’ll love to solve your problem.

Here’re some Articles you might be Interested:

GENERATE QR CODE USING ASP.NET CORE

HOW TO CREATE SOAP WEB SERVICES IN DOTNET CORE

Author

I'm passionate about learning new technologies as well as mentoring and helping others get started with their programming career. This blog is my way of giving back to the Community.

Write A Comment