This content is archived. See latest version here

Last updated: Oct 21 2014

Introduction

Meta models can be exported and imported, and you can also compare two meta models which is useful during these operations. Refer to Business Meta Model for more information about meta models in general.

All classes referred to here are available in the Mediachase.BusinessFoundation.Data.Meta.Schema namespace.

Exporting meta models

A meta model can be exported to a file. Use the MetaModelGenerator class to save a meta model to a file. 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#
// Create MetaModelGenerator
                MetaModelGenerator generator = new MetaModelGenerator();
                // Use generator.SelectedElements to export custom elements
                try
                {
                // Open DataContext
                DataContext.Current = new DataContext(connectionString);
                // Load Save Commands
                SchemaDocument schema = new SchemaDocument();
                schema.LoadDefault();
                generator.Schema = schema;
                // Load Save Commands
                XmlDocument xmlOutput = generator.Generate();
                xmlOutput.Save(filePath);
                }
                catch (Exception ex)
                {
                }

Comparing meta models

Before importing a meta model, you should compare meta models and create synchronization commands. An existing meta model can be exported and compared with the one that you are about to import, before the actual import.

Example: Comparing two meta models and create synchronization script

C#
try
                {
                // Load Imported Meta Model
                List<ModuleManifest> installedModules = new List<ModuleManifest>();
                XmlDocument xmlSrcMetaModel = new XmlDocument();
                xmlSrcMetaModel.Load(srcMetaModelPath);

                // Load Modules From Source Meta Model
                foreach (XmlNode xmlManifestNode in
                xmlSrcMetaModel.SelectNode("//mediachase.businessFoundation.data.meta/description/moduleManifests/moduleManifest"))
                {
                ModuleManifest manifest =
                McXmlSerializer.GetObject<ModuleManifest>(xmlManifestNode.OuterXml);
                installedModules.Add(manifest);
                }

                // Load SchemaDocument
                SchemaDocument schema = new SchemaDocument();
                schema.LoadDefault(installedModules.ToArray());

                // Load Original meta model
                XmlDocument xmlDestMetaModel = new XmlDocument();
                xmlDestMetaModel.Load(destMetaModelPath);

                // Compare two meta models
                SyncCommand\[] syncCommands = MetaModelSync.FindModifications(schema, xmlSrcMetaModel, xmlDestMetaModel);

                // Save sync commands to file
                McXmlSerializer.SaveObjectToFile<SyncCommand\[\]>(outputSyncFilePath, syncCommands);
                }
                catch (Exception ex)
                {
                }

Importing meta models

When importing a meta model you should first export the original meta model, compare it with the meta model to be imported, and create synchronization commands as previously described. Finally you then execute the synchronization commands.

Example: Loading and executing synchronization commands

C#
try
                {
                // Open DataContext
                DataContext.Current = new DataContext(connectionString);

                // Load Sync Commands
                SchemaDocument schema = new SchemaDocument();
                schema.LoadDefault();

                // Load Sync Commands
                SyncCommand\[] syncCommands =
                McXmlSerializer.GetObjectFromFile<SyncCommand\[]>(filePath);

                // Apply Sync Command
                using (TransactionScope tran = DataContext.Current.BeginTransaction())
                {
                MetaModelSync.Execute(schema, syncCommands);

                tran.Commit();
                }
                }
                catch (Exception ex)
                {
                }

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/