-
Notifications
You must be signed in to change notification settings - Fork 21
Design Mode
SMO supports the concept of "design mode" for most objects. Any object that implements ISfcSupportsDesignMode
supports some level of offline manipulation.
There's something of a trick needed to build a SMO hierarchy in design mode, though.
Setting an object's Parent requires verifying that the new parent supports that child, and unfortunately such validation involves querying the ServerVersion property of the server at the root of the hierarchy. Also, the server itself has to be in offline mode, and offline mode is really an SFC concept, so setting it requires some SFC interface usage.
To initialize a SMO hierarchy in design mode, use code like this:
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
// setting ServerVersion allows setting Database.Parent without a server query to get the version
// set TrueName to ensure the URN that identifies the Server is complete
var serverConnection = new ServerConnection() { ServerVersion = new ServerVersion(15, 0), TrueName="designMode" };
var server = new Management.Smo.Server(serverConnection);
// design mode requires an offline connection
(server as ISfcHasConnection).ConnectionContext.Mode = SfcConnectionContextMode.Offline;
var database = new Database()
{
Parent = server
};
Now you can set properties and add child objects to the database. One limitation of design mode is that the Server will assume its database type is DatabaseEngineType.Standalone, so it can't be used for Azure DW or Azure SQL DB.
There's an open issue #27 to fix scripting of such a hierarchy. There are probably lots of bugs hidden by that top level one, but we can chip away at it over time.