Skip to content

.NET Core Examples

Roger Zander edited this page Nov 21, 2018 · 4 revisions

JainDB can be used as an embedded noSQL (JSON) database for .Net Core applications.

Required Nuget references:

Additionally, you need at least one Storage-Provider

You can also add multiple providers. In this case the providers are loaded in the order of the DLL Name (jaindb.storage.*.dll). The Memory Provider should always start first to act as a chache.

Note: Every provider requires a .json file with the same name as the DLL. If the File does not exists, a default configuration file will be generated on the first start.

Tip: start with the jaindb.storage.FileStore provider as it stores data in the local disk and needs no further configuration.

Before you start, enable "CopyLocalLockFileAssemblies" in your .csproj File:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

This will enforce to copy all required DLL's (including the Storage Providers) to your output directory. Otherwise the Providers may store files in a directory outside of your project...

C# example:

        static void Main(string[] args)
        {
            Console.WriteLine("loading Storage-Providers:");
            jaindb.jDB.loadPlugins(); //never forget this step !
            Console.WriteLine("");

            //Genereate a test JSON
            string sJson = "{ \"prop1\": \"id1\", \"prop2\" : \"bla bla\" }";
            
            //Store JSON and set Object-Identifier to "OBJ1"
            string sHash = jaindb.jDB.UploadFull(sJson, "OBJ1");

            //Get OBJ1 back from JainDB
            var jObj1 = jaindb.jDB.GetFull("OBJ1");

            //Add an additional key Attribute
            jObj1.Add("#name", "Object1");

            //Upload JSON again
            string sHash2 = jaindb.jDB.UploadFull(jObj1.ToString(), "OBJ1");

            //Get OBJ1 back from JainDB by using the key #name
            string sID = jaindb.jDB.LookupID("#name", "Object1");
            var jObj2 = jaindb.jDB.GetFull(sID);
            Console.WriteLine(jObj2.ToString(Newtonsoft.Json.Formatting.Indented));

            Console.ReadKey();
        }
Clone this wiki locally