Getting Started with LiteDB

Every project you do will have data and eventually you’re going to need to store that data somewhere.

And config files are such a pain to manage (and also so early 2000s) so as much as possible I try to use a database or some unit of storage.

And like anything you build, once you start building it, it takes on a life of it’s own that you twist and turn and wrench into something useful until one day you look at it and go – maybe storing our configuration in a 25,000 line XML file wasn’t the best idea?

So if it’s going to be a bad idea down the road, why not start doing the right thing from the get go – enter LiteDb.

LiteDb is an incredibly easy to use, mongo-like db that you can get up and running with in minutes.  Honestly – you can get data into a LiteDb database faster than you could a config file.

Step 1: Install LiteDb

Like any good library, this can be done via Nuget.  Create a project and run the following command.

Install-Package LiteDB

And that’s done – now you have a database engine in your project.

Step 2: Create a Database

I remember having to do this with SQL Scripts that I’d send through C# code to a previously installed SQL Express or some other instance of a database that would always be some complicated endeavour fraught with errors and typos.

Here’s the code to do this with LiteDb.

 using (var database = new LiteDatabase("mydatabase.db"))

//At this point, your database is now created and ready to use.


You have now created a LiteDb database that resides in your currently executing directory.  If it already existed, you have now loaded that database.

Note: If you want to specify a location for your database, you can do this in your config file along with a number of other options.

For each database file that you create (.db), you’re looking at max storage limit of 2GB per file.  This might initially seem small to store all your data but remember – what’s your end goal?  Is it to get up and running quickly with a data structure that easily supports POCO or typed formats or is it to run Amazon?

Even if your goal is to do something large and you want to keep using LiteDb on the cheat – create multiple data files if you need to – but always remember to ask yourself – “Should I be doing this?  Will this scale?”

In terms of getting yourself up and running quickly with a database that can span desktop, mobile, web, UWP, Xamarin IOS or Android – LiteDb is a great way to get started (and infinitely better than any configuration file).



Post A Reply

%d bloggers like this: