You can export and import meta-models. You also can compare two meta-models, which is useful during these operations.
The following classes referred are available in the Mediachase.BusinessFoundation.Data.Meta.Schema namespace.
Exporting meta-models
Export a meta-model to a file with the MetaModelGenerator class. By default, MetaModelGenerator exports all elements; use the SelectedElements property of the MetaModelGenerator class to export custom elements.
Example: Exporting a meta-model to a file
C#
MetaModelGenerator generator = new MetaModelGenerator();
try
{
DataContext.Current = new DataContext(connectionString);
SchemaDocument schema = new SchemaDocument();
schema.LoadDefault();
generator.Schema = schema;
XmlDocument xmlOutput = generator.Generate();
xmlOutput.Save(filePath);
}
catch (Exception ex)
{
}
Comparing meta-models
Before you import a meta-model, you should compare meta-models and create synchronization commands. You can export and compare an existing meta-model with the one that you are about to import before actually importing it.
Example: Comparing two meta-models and create synchronization script
C#
try
{
List<ModuleManifest> installedModules = new List<ModuleManifest>();
XmlDocument xmlSrcMetaModel = new XmlDocument();
xmlSrcMetaModel.Load(srcMetaModelPath);
foreach (XmlNode xmlManifestNode in
xmlSrcMetaModel.SelectNode("//mediachase.businessFoundation.data.meta/description/moduleManifests/moduleManifest"))
{
ModuleManifest manifest =
McXmlSerializer.GetObject<ModuleManifest>(xmlManifestNode.OuterXml);
installedModules.Add(manifest);
}
SchemaDocument schema = new SchemaDocument();
schema.LoadDefault(installedModules.ToArray());
XmlDocument xmlDestMetaModel = new XmlDocument();
xmlDestMetaModel.Load(destMetaModelPath);
SyncCommand\[] syncCommands = MetaModelSync.FindModifications(schema, xmlSrcMetaModel, xmlDestMetaModel);
McXmlSerializer.SaveObjectToFile<SyncCommand\[\]>(outputSyncFilePath, syncCommands);
}
catch (Exception ex)
{
}
Importing meta-models
When import a meta-model, follow these steps.
- Export the original meta-model.
- Compare it with the meta-model to be imported.
- Create synchronization commands (as described in the previous section).
- After you compare, execute the synchronization commands.
Example: Loading and executing synchronization commands
C#
try
{
DataContext.Current = new DataContext(connectionString);
SchemaDocument schema = new SchemaDocument();
schema.LoadDefault();
SyncCommand\[] syncCommands =
McXmlSerializer.GetObjectFromFile<SyncCommand\[]>(filePath);
using (TransactionScope tran = DataContext.Current.BeginTransaction())
{
MetaModelSync.Execute(schema, syncCommands);
tran.Commit();
}
}
catch (Exception ex)
{
}