mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

Assembly Name change #2170

Closed bluekuen closed 2 years ago

bluekuen commented 2 years ago

Discussed in https://github.com/mbdavid/LiteDB/discussions/2168

Originally posted by **bluekuen** April 7, 2022 Hello together, like the title says did my Type name change from DeviceSetMobile to DeviceSet.Mobile my Assembly Name to DeviceSet and now i got these Exceptions from all of my database classes which are stored: LiteDB.LiteException: 'Type 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile' not found in current domain (_type format is 'Type.FullName, AssemblyName'). Is there an simple and easy way to announce assembly name changes? I'm thankful for every answer! Kind regards bluekuen
mr-sven commented 2 years ago

Hi, you can use the LiteDB studio to update the type ref in your entries. If you have it already in Prod you can use the statements in a migration function.

bluekuen commented 2 years ago

Hello, thanks for the answer!

To clarify: I only save User in my database so i only have a User collection. These users store Devices in a list. Yes its already in prod and i already tried it this way:

var updateSQL = "UPDATE User SET _type = " + "REPLACE(_type, 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile', 'DeviceSet.Mobile.Models.BleDevice, DeviceSet') " + "WHERE _type LIKE 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile'";

_database.Execute(updateSQL);

But it didn't work somehow. Am i doing it wrong?

Thanks in advance!

mr-sven commented 2 years ago

ok, it looks like you try to set the _type of the user, not the the type of entry of the users device list. Can you paste a sample text output from LiteDB Studio?

bluekuen commented 2 years ago

There's just a zero: image

mr-sven commented 2 years ago

I mean a user object

bluekuen commented 2 years ago

Ok i see. But how am i able to update the Devices inside the User? if i try update User.Devices it would be the list right? Also User.Devices can't be updated.

mr-sven commented 2 years ago

make a backup, try something like: UPDATE User SET $.Devices[@._type = 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile']._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'

bluekuen commented 2 years ago

thanks so much for your help! I tried the command you provided but get following error:

Unexpected token $ in position 17.

UPDATE User SET $.Devices[@._typ -----------------^

Do i need to escape with an escape char?

mr-sven commented 2 years ago

maybe omit? UPDATE User SET Devices[@._type = 'DeviceSetMobile.Models.BleDevice, DeviceSetMobile']._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'

mr-sven commented 2 years ago

hm, very tricky

mr-sven commented 2 years ago

maybe you can try convert it with a Untyped collection:

using(var db = new LiteDatabase("mydb.db"))
{
    var col = db.GetCollection("User");

    foreach (var user in col.FindAll())
    {
        foreach (var device in user["Devices"])
        {
            if (device["_type"] == "DeviceSetMobile.Models.BleDevice, DeviceSetMobile")
            {
                device["_type"] = "DeviceSet.Mobile.Models.BleDevice, DeviceSet";
            }
        }
        col.Update(user);
    }
}
bluekuen commented 2 years ago

Sadly didn't work either. I tried it this way:

UPDATE User SET Devices = Devices[*]._type = 'DeviceSet.Mobile.Models.BleDevice, DeviceSet'

But gave me this error:

Left expression MAP($.Devices[*]=>@._type) returns more than one result. Try use ANY or ALL before operant.

mr-sven commented 2 years ago

Untyped collection also not working?

bluekuen commented 2 years ago

Untyped collection did work! Thank you very very much! You saved my day!