Create an exact copy of Analysis Services database with AMO

Viewed 150

I'm trying to automate the creation of an exact copy of an Analysis Services database using Microsoft.AnalysisServices namespace. My code is:

using (var server = new Server())
{
    server.Connect(connString);
    var newDb = server.Databases.GetByName(dbName).Clone();
    newDb.Name = newDbName;
    newDb.ID = server.Databases.GetNewID();
    server.Databases.Add(newDb);
    newDb.Update(UpdateOptions.ExpandFull);
    server.Disconnect();
}

However it seems it creates an empty database instead because from SSMS i'm not able to see any tables, datasources and similar (check my screenshot here).

Is there a way to fix this? Thanks.

1 Answers

@gmarchi You need to clone the child Model object using Model.clone() method as well to make to make it work. You can add Model using code like below -

        var connString = "Provider=MSOLAP;Data Source=asazure://westus2.asazure.windows.net/xxxxxxxxxxxx:rw";
        var dbName = "AW Internet Sales";
        var newDbName = "CloneDb";
        
        using (var server = new Server())
        {
            server.Connect(connString);
            var newDb = server.Databases.GetByName(dbName).Clone();
            var newModel = server.Databases.GetByName(dbName).Model.Clone();
            
            newDb.Name = newDbName;
            newDb.ID = server.Databases.GetNewID();
            newDb.Model = newModel;

            server.Databases.Add(newDb);
            newDb.Update(UpdateOptions.ExpandFull);
            server.Disconnect();
        }

Please find below the screenshot after the code run. enter image description here

Please let me know if you have any questions. Thanks.

Related