Litedb: What is the best way to handle schema/class changes?

Created on 24 Oct 2016  Â·  10Comments  Â·  Source: mbdavid/LiteDB

What is the best way of handling changes or rename into a class property?

e,g, from

public class Person
{
    public string Name { get; set; }
}

to

public class Person
{
    public string NickName { get; set; }
}
question

Most helpful comment

It would be nice to publish it in wiki as _Migrations_ :)

All 10 comments

Hi @dodyg, if your change is just a simple rename, you can rename only property class and do not rename document. Just use:

[BsonField("Name")]
public string NickName { get; set; }

or using BsonMapper

BsonMapper.Global.Entity<Person>()
    .Field(x => x.NickName, "Name");

But, if your have more complex changes, use DbVersion to track database version and use BsonDocument to get old values before change.

var db = new LiteDatabase(....);

if(db.DbVersion == 0)
{
    var col = db.GetCollection("Person");
    foreach(var doc in col.FindAll())
    {
        col["NickName"] = col["Name"];
        col.RemoveKey("Name");
        col.Update(doc);
    }
    db.DbVersion = 1;
}

It would be nice to publish it in wiki as _Migrations_ :)

Related to this question, how would I add a new property to a class that is also a class? I tried adding in a new key with the value being an empty bson document, but it then gave an error when it tried to deserialize it into the class. What do I need to add to the bson document to make it register as being the new class?

I'm trying to avoid direct references to the class, which is why I didn't just serialize a new instance of it, because I want to manage my database updates in a way that is separate from the schema so as classes are further modified it doesn't break older updates.

Hi @emrys90, can you paste you code here? Its a DbRef ?

So let's say I have two classes,
public class ClassA {}
public class ClassB {}

I change ClassB to have a reference to ClassA
public class ClassB
{
public ClassA ClassA { get; set; }
}

How do I write an update that will set the ClassA property on ClassB?

How do you do this in v3? db.DbVersion no longer exists...

Now is db.Engine.UserVersion

Em sáb, 7 de jan de 2017 às 15:53, emrys90 notifications@github.com
escreveu:

How do you do this in v3? db.DbVersion no longer exists...

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/mbdavid/LiteDB/issues/328#issuecomment-271098875, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABFaLeWvUO5hxRb7dqsd_E3WNeArVqzyks5rP9E1gaJpZM4KecSJ
.

>

Sent from Gmail Mobile

Ayuda como mando los datos que tengo en mi base a otra clase para que pueda utilizarlos

For anyone still looking at this issue, maybe this page would be the best reference to look at regarding DB version management/migration:

https://github.com/mbdavid/LiteDB/wiki/LiteDB-v1 _[Edit] this wiki was for V1 and doesn't seem applicable anymore in latest version._

Otherwise, from the example mentioned earlier in this thread - seems like LiteDB syntax has changed to the following (in case it helps anyone else):

if (db.Engine.UserVersion == 0)
{
    var col = db.GetCollection("Person");
    foreach (var doc in col.FindAll())
    {
        doc["NickName"] = doc["Name"];
        doc.Remove("Name");
        col.Update(doc);
    }
    db.Engine.UserVersion = 1;
}

`public LiteDatabase DB { get; set; }
public List Configurations { get; set; }
public string myPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    public MyRepo()
    {
        try
        {
            DB = new LiteDatabase("filename = "+@myPath + "\\" + "iksg.db"+ "; upgrade = true");
            if (DB.UserVersion == 0)
            {
                var col = DB.GetCollection("configurations");
                foreach (var doc in col.FindAll())
                {
                    col.Update(doc);
                }
                DB.UserVersion = 1;
            }
            Configurations = DB.GetCollection<Configuration>("configurations").FindAll().ToList<Configuration>();
        }
        catch (Exception ex)
        {
            throw ex;
        }`

I've made many modifications in my Configuration class and in this line i get the exception : "Invalid LCID code"
DB = new LiteDatabase("filename = "+@myPath + "\\" + "iksg.db"+ "; upgrade = true");
can you please help me solve this problem

Was this page helpful?
0 / 5 - 0 ratings