How to migrate an existing database to the Content Repository

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.4
  • Enterprise
  • Community
  • Planned

Overview

SenseNet ECM is built on a highly flexible Content Repository that is able to store all sorts of Content. That means you can define your own content types with any number of metadata fields and build your business logic on top of these objects. You do not have to define and manage custom database tables to store custom data in SenseNet - that is what the Content Repository does for you.

In this article developers may learn how to migrate an existing database (including metadata and relations) to the SenseNet Content Repository easily, with a few lines of code.

Prerequisites

To be able to create custom content in SenseNet, you will have to define content types and a place where you want to put your content.

Define content types

Firts you will have to define the content types you want to work with. As you already have an existing database with a schema, this should be fairly easy. In this example we will migrate cars from a car database. A car has a few metadata fields, for example:

  • Make
  • Model
  • Price

To see examples for a content type definition in SenseNet, please check out the following article:

There is already a sample Car content type in the default SenseNet installation, we will work with that.

Create the container for new content

You will have to create container (e.g. a list, a workspace or a folder) somewhere in the Content Repository to store these new elements (in our case, Cars). As an example we create a new Custom List called Cars under the default site. The following is a screenshot made in Content Explorer where we do these administrative tasks.

Cars list

To be able to create cars here, we will have to allow the Car type to be created in this container. To achieve that, please click on the Edit button on the toolbar to enter the edit page of the Cars list. Modify the allowed child types list there to contain the Car content type.

Cars list allowed types

The migration tool

The easiest way to migrate existing content to the Content Repository is to create a simple command line tool with the help of the Client library. It connects to an existing SenseNet installation through our OData REST API, so please make sure you have a running portal - for example on http://localhost. The references you will have to add to the command line tool are the following:

  • SenseNet.Client.dll (found in the Tools folder of the SenseNet installation)
  • Newtonsoft.Json.dll (either from Nuget.org or from inside the SenseNet installation)

Of course you will also have to reference any other library that you need to load your existing items from your existing database, for example Entity Framework.

Cars table

The following example uses a simple SQL query to load cars from the sample Cars table. The method to access your custom data of course out of scope here, we only show this as an example. The important part of the sample code here is the way you can create new items (in this case Cars) in the Content Repository.

static void Main(string[] args)
{
    // initialize the client context to be able to connect to a SenseNet Content Repository
    ClientContext.Initialize(new[]
    {
        new ServerContext
        {
            Url = "http://localhost",
            Username = "admin",
            Password = "admin"
        }
    });
 
    try
    {
        MigrateAsync().Wait();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}
 
private static async Task MigrateAsync()
{
    using (var conn = new SqlConnection(ConnectionString))
    {
        await conn.OpenAsync();
 
        using (var command = new SqlCommand("SELECT * FROM Cars", conn))
        {
            using (var reader = await command.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    var id = reader.GetInt32(0);
                    var make = reader.GetString(1);
                    var model = reader.GetString(2);
                    var price = reader.GetInt32(3);
 
                    // Build a new content in memory and fill custom metadata fields. No need to create
                    // strongly typed objects here as the client Content is a dynamic type.
                    // Parent path is a Content Repository path, e.g. "/Root/Sites/Default_Site/Cars"
                    dynamic car = Content.CreateNew(ParentPath, "Car", "Car-" + id);
                    car.Make = make;
                    car.Model = model;
                    car.Price = price;
 
                    // save it through the HTTP REST API
                    await car.SaveAsync();
 
                    Console.WriteLine("Car-" + id + " saved.");
                }
            }
        }
    }
}

Of course the example above does not contain proper error handling and is not very efficient as it saves content items one after the other instead of creating them in parallel, but we wanted to demonstrate only the core concept. For more advanced data migration steps (e.g. creating relations between to types of content) please check out the examples in the following article:

Results

After executing the migration tool, you will be able to see the newly created items in the Cars list in Content Explorer:

Cars list filled


Related links

References

There are no external references for this article.