in SampleConsoleApp/ModelEndToEnd.cs [42:97]
public static void Run()
{
string dacpacPath = "relativePath.dacpac";
// Note that you could read scripts from a file or use TSqlScript objects that are taken from other models.
// Hand-crafting TSqlScript is quite awkard but could be done programmatically (we do it internally).
// If you need examples for this let us know and we can look into that too.
string[] scripts = new[]
{
"CREATE TABLE t1 (c1 NVARCHAR(30) NOT NULL)",
"CREATE TABLE t2 (c2 INT NOT NULL)",
"CREATE TABLE t3 (c3 INT NOT NULL)",
"CREATE TABLE t4 (c4 INT NOT NULL)",
};
using (TSqlModel model = new TSqlModel(SqlServerVersion.Sql110, new TSqlModelOptions { }))
{
// Adding objects to the model.
foreach (string script in scripts)
{
model.AddObjects(script);
}
ReadTheModel(model);
CopyFromTheModel(model);
// save the model to a new .dacpac
// Note that the PackageOptions can be used to specify RefactorLog and contributors to include
DacPackageExtensions.BuildPackage(
dacpacPath,
model,
new PackageMetadata { Name = "MyPackageName", Description = "This is usually ignored", Version = "1.0" },
new PackageOptions()
);
}
// Load from a dacpac
using (TSqlModel modelFromDacpac = new TSqlModel(dacpacPath))
{
// Show that all the elements were saved successfully
ReadTheModel(modelFromDacpac);
// You can update the model in the dacpac. Other parts of a dacpac can't be updated yet (pre/post deployment scripts)
modelFromDacpac.AddObjects("CREATE VIEW V1 AS SELECT * FROM T1");
using (DacPackage dacPackage = DacPackage.Load(dacpacPath,
DacSchemaModelStorageType.Memory,
FileAccess.ReadWrite))
{
DacPackageExtensions.UpdateModel(dacPackage, modelFromDacpac, null);
}
}
Console.WriteLine("Press any key to finish");
Console.ReadKey();
}