Compartir a través de


How to: Make Changes to a Model using Metadata

You can create elements and links by using metadata. This approach is an alternative to using the new keyword (in C#). You would typically use metadata when you do not know what type of object to create when you compile the code.

To create elements by using metadata

  1. Use a Find method of the DomainDataDirectory member of the store to retrieve the metadata information.

    Find methods include FindDomainClass, FindDomainRelationship, and FindDomainProperty.

  2. Use the ElementFactory member of the store to create an element or a link for the type of object that you want to create.

Example

In the following example, three instances of a particular type of domain class are created.

using (Transaction txCreateElemMetadata =  
      store.TransactionManager.BeginTransaction("Create elements with reflection"))
    {              
        DomainClassInfo dci =
        store.DomainDataDirectory.FindDomainClass(typeof(Library));
        store.ElementFactory.CreateElement(dci.Id);
        store.ElementFactory.CreateElement(dci.Id);
        store.ElementFactory.CreateElement(dci.Id);
        txCreateElemMetadata.Commit();
    }
    // Three new Library elements are in the store
    ReadOnlyCollection<ModelElement> foundelements = store.ElementDirectory.FindElements(Library.DomainClassId);
    int totallibraries = foundelements.Count; 

See Also

Concepts

Working with Metadata in the Store

Domain-Specific Language Tools Glossary

Reference

TransactionManager

DomainDataDirectory

ElementFactory