This content is archived. See latest version here

Last updated: Mar 31 2014

Business Foundation (BF) supports referencing between meta classes which is useful when creating relations between different types of business objects. The following types of references can be created:

  • 1-N reference
  • N-N reference (bridge)
  • Aggregation reference

The various reference types are described in more detail below.

When creating meta class references, remember that a meta model can only be modified in Design mode. Refer to the MetaClassManager Class section for more information.

Creating a 1-N reference

The 1-N reference type will create a reference between two meta classes.

Be careful to define the TitleFieldName property of the MetaClass class with the correct string field name if you want to create a reference to this meta class.

In this example we will create a reference between the two meta classes "Project" and "Task".

1-N Reference

Call the CreateReference method of the MetaClass class, passing parent meta class, reference name, friendly name and is nullable flag, to create a reference to the parent meta class.

Example: Creating a reference between Project and Task

C#
// Open the Meta model edit scope
            using (MetaClassManagerEditScope scope = DataContext.Current.MetaModel.BeginEdit())
            {
            // Create reference
            DataContext.Current.GetMetaClass("Task").CreateReference(
            DataContext.Current.GetMetaClass("Project"), "Project", "Project", true)):
            // Save Changes
            scope.SaveChanges();
            }

The CreateReference method adds two fields to the meta class:

  • Reference Id reference on the parent primary key, named "[ReferenceName]Id". This is used to get/set the reference to the parent object.
  • Reference Title a read-only referenced field property to the parent title, named "[ReferenceName]". This is used to get the title of the parent object.

When you update Reference Id, Reference Title will be automatically updated.

More than one reference can be created between two meta classes.

Creating a reference field

After creating a reference, you can add any field from the parent meta class as Reference field.

Reference Field

All referenced fields are read-only. If a reference is not set, the reference field returns null. When you update When you update the Reference Id, Reference Title will be automatically updated.

Call the CreateReferencedField method of the MetaClass class, passing the reference field to the parent meta class, and the parent field and field name to add a referenced field to a meta class

Creating a back reference

After creating a reference in the parent meta class, you can create a Back reference field on an existing reference. The back reference field returns a collection of child objects that have a reference to the current object.

Back Reference

Call the CreateBackReference method of the MetaClass class, passing reference field and field name to add a back reference field.

Creating an N-N reference

The N-N reference type is also called a "bridge", which is meta class with two reference on class #1 and class #2.

Remember to define the TitleFieldName property of the MetaClass class in both meta classes.

n this example we will create a bridge between the two meta classes "Project" and "Union".

Bridge

Call the CreateBridge method of the MetaClassManager class, passing class #1 and class #2 to create a bridge between the two meta classes. The bridge meta class will be visible in the collection of meta classes like the original meta class, but it will have an IsBridge property with the value "true". You can add a meta field or delete a bridge meta class like any other meta class.

Creating an aggregation reference

Aggregation is slightly more complex than references. You typically use it to enable the parent entity object that has a collection of child entity objects, where one element is marked as default. The default children entity object is loaded when the parent object is loaded, and will be available from the property. The properties of children entity objects can be modified and will be saved when you save the parent object. When you delete a parent object, all aggregated objects will be automatically removed.

Be careful to define the TitleFieldName property of the MetaClass class with the correct string field name if you want to create an aggregation reference to this meta class.

In this example we have the meta classes "Client" and "Address", where the Client meta class aggregates the Address meta class. The client has many addresses but one is marked as default, and is available from the Client.Address property. The Address property returns an entity object, and you can update the default address properties as client properties. When you save Client, the default Address will be automatically saved.

Aggregation

Call the CreateAggregation method of the MetaClass class, passing the children meta class to add an aggregation to a meta class. The method adds a new meta field to a parent meta class, which can be used to set and update an aggregated object.

Example: Creating an aggregated reference

C#
public MetaField CreateAggregation(string name, string friendlyName, string childMetaClassName,string elementContainerRefFieldFriendlyName,string defaultElementFieldFriendlyName)
            {
            MetaField retVal = this.MetaClass.CreateAggregation(childMetaClassName, name, friendlyName,
            elementContainerRefFieldFriendlyName,
            defaultElementFieldFriendlyName);
            return retVal;
            }

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/