Using the schema creator


Summary This topic shows how to use data elements and the schema creator to create a geodatabase table.

In this topic


Creating a table data element

The first step in creating schema with the schema creator is creating data elements for the geodatabase objects. The following code example shows how to create a table data element with three fields:
[C#]
// Create a fields collection.
IFieldsEdit fieldsEdit = new FieldsClass();

// Create an Object ID field.
IFieldEdit oidFieldEdit = new FieldClass();
oidFieldEdit.Name_2 = "OBJECTID";
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.AddField(oidFieldEdit);

// Create a text field.
IFieldEdit textFieldEdit = new FieldClass();
textFieldEdit.Name_2 = "NAME";
textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
textFieldEdit.Length_2 = 100;
fieldsEdit.AddField(textFieldEdit);

// Create a date field.
IFieldEdit dateFieldEdit = new FieldClass();
dateFieldEdit.Name_2 = "BIRTHDATE";
dateFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
fieldsEdit.AddField(dateFieldEdit);

// Create the table data element and apply the fields collection.
IDETable deTable = new DETableClass();
deTable.OIDFieldName = "OBJECTID";
deTable.HasOID = true;
deTable.Fields = fieldsEdit;
[VB.NET]
' Create a fields collection.
Dim fieldsEdit As IFieldsEdit = New FieldsClass

' Create an Object ID field.
Dim oidFieldEdit As IFieldEdit = New FieldClass
oidFieldEdit.Name_2 = "OBJECTID"
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID
fieldsEdit.AddField(oidFieldEdit)

' Create a text field.
Dim textFieldEdit As IFieldEdit = New FieldClass
textFieldEdit.Name_2 = "NAME"
textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString
textFieldEdit.Length_2 = 100
fieldsEdit.AddField(textFieldEdit)

' Create a date field.
Dim dateFieldEdit As IFieldEdit = New FieldClass
dateFieldEdit.Name_2 = "BIRTHDATE"
dateFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate
fieldsEdit.AddField(dateFieldEdit)

' Create the table data element and apply the fields collection.
Dim deTable As IDETable = New DETableClass
deTable.OIDFieldName = "OBJECTID"
deTable.HasOID = True
deTable.Fields = fieldsEdit

Setting data element properties

After the table data element has been created, apply properties from IDataElement and IDEGdbTable. Note that the "OC" CatalogPath property in the following code example means "object class."
[C#]
// Set the DETable's data element properties.
IDataElement dataElement = (IDataElement)deTable;
dataElement.Name = "PEOPLE";
dataElement.FullPropsRetrieved = true;
dataElement.CatalogPath = "/OC=PEOPLE";

// Set the DETable's GDB data element properties. Use a class description
// to get the table's instance CLSID.
IDEGdbTable deGdbTable = (IDEGdbTable)deTable;
IObjectClassDescription objClassDesc = new ObjectClassDescriptionClass();
UID objectUID = objClassDesc.InstanceCLSID;
deGdbTable.CLSID = Convert.ToString(objectUID.Value);
[VB.NET]
' Set the DETable's data element properties.
Dim dataElement As IDataElement = CType(deTable, IDataElement)
dataElement.Name = "PEOPLE"
dataElement.FullPropsRetrieved = True
dataElement.CatalogPath = "/OC=PEOPLE"

' Set the DETable's GDB data element properties. Use a class description
' to get the table's instance CLSID.
Dim deGdbTable As IDEGdbTable = CType(deTable, IDEGdbTable)
Dim objClassDesc As IObjectClassDescription = New ObjectClassDescriptionClass
Dim objectUID As UID = objClassDesc.InstanceCLSID
deGdbTable.CLSID = Convert.ToString(objectUID.Value)

Creating a schema creator

With the data element's properties fully populated, an array of data elements can be created to pass into a schema creator. In this case, a single object class is being created, but if multiple datasets were being created, they would all be added to the same array. Although the following code example does not use domains, to include them, a second array should be created specifically for domains.
[C#]
// Create an ESRI Array to contain the dataset data element or elements.
IArray dataElementsArray = new ArrayClass();
dataElementsArray.Add(deTable);
[VB.NET]
' Create an ESRI Array to contain the dataset data element or elements.
Dim dataElementsArray As IArray = New ArrayClass
dataElementsArray.Add(deTable)
The next step is to create a schema creator and generate name mappings for the datasets and domains to be created. Name mappings are generally used in data conversion to map schema between two data sources where certain aspects of the original schema might not be valid in the target data source. In this case, an enumerator of name mappings defines the schema that will be created in the geodatabase.
[C#]
// Instantiate a new schema creator and generate an enumerator of name mappings.
IGdbSchemaCreator gdbSchemaCreator = new GdbSchemaCreatorClass();
IEnumNameMapping enumNameMapping = null;
Boolean conflictsFound = false;
gdbSchemaCreator.GenerateNameMapping(workspace, dataElementsArray, null, out
    enumNameMapping, out conflictsFound);
[VB.NET]
' Instantiate a new schema creator and generate an enumerator of name mappings.
Dim gdbSchemaCreator As IGdbSchemaCreator = New GdbSchemaCreatorClass
Dim enumNameMapping As IEnumNameMapping = Nothing
Dim conflictsFound As Boolean = False
gdbSchemaCreator.GenerateNameMapping(workspace, dataElementsArray, Nothing, enumNameMapping, conflictsFound)
If the final outbound parameter of GenerateNameMapping indicates that conflicts were detected, it is the application's responsibility to modify the conflicting name mapping objects so they are appropriate for the target data source. For information on how this is done, see the "Handling name conflicts" section in Copying and pasting geodatabase datasets.
If conflicts are not detected, the schema creator can be given the name mappings and the creation process can begin. See the following code example:
[C#]
// If conflicts were not found, generate the schema.
if (!conflictsFound)
{
    gdbSchemaCreator.CreateSchema(workspace, enumNameMapping);
}
[VB.NET]
' If conflicts were not found, generate the schema.
If Not conflictsFound Then
    gdbSchemaCreator.CreateSchema(workspace, enumNameMapping)
End If

Complete code examples

The following code examples combine the previous code examples into single methods:
[C#]
private static void SchemaCreationExample(IWorkspace workspace)
{
    // Create a fields collection.
    IFieldsEdit fieldsEdit = new FieldsClass();

    // Create an Object ID field.
    IFieldEdit oidFieldEdit = new FieldClass();
    oidFieldEdit.Name_2 = "OBJECTID";
    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
    fieldsEdit.AddField(oidFieldEdit);

    // Create a text field.
    IFieldEdit textFieldEdit = new FieldClass();
    textFieldEdit.Name_2 = "NAME";
    textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
    textFieldEdit.Length_2 = 100;
    fieldsEdit.AddField(textFieldEdit);

    // Create a date field.
    IFieldEdit dateFieldEdit = new FieldClass();
    dateFieldEdit.Name_2 = "BIRTHDATE";
    dateFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
    fieldsEdit.AddField(dateFieldEdit);

    // Create the table data element and apply the fields collection.
    IDETable deTable = new DETableClass();
    deTable.OIDFieldName = "OBJECTID";
    deTable.HasOID = true;
    deTable.Fields = fieldsEdit;

    // Set the DETable's data element properties.
    IDataElement dataElement = (IDataElement)deTable;
    dataElement.Name = "PEOPLE";
    dataElement.FullPropsRetrieved = true;
    dataElement.CatalogPath = "/OC=PEOPLE";

    // Set the DETable's GDB data element properties. Use a class description
    // to get the table's instance CLSID.
    IDEGdbTable deGdbTable = (IDEGdbTable)deTable;
    IObjectClassDescription objClassDesc = new ObjectClassDescriptionClass();
    UID objectUID = objClassDesc.InstanceCLSID;
    deGdbTable.CLSID = Convert.ToString(objectUID.Value);

    // Create an ESRI Array to contain the dataset data element or elements.
    IArray dataElementsArray = new ArrayClass();
    dataElementsArray.Add(deTable);

    // Instantiate a new schema creator and generate an enumerator of name mappings.
    IGdbSchemaCreator gdbSchemaCreator = new GdbSchemaCreatorClass();
    IEnumNameMapping enumNameMapping = null;
    Boolean conflictsFound = false;
    gdbSchemaCreator.GenerateNameMapping(workspace, dataElementsArray, null, out
        enumNameMapping, out conflictsFound);

    // If conflicts were not found, generate the schema.
    if (!conflictsFound)
    {
        gdbSchemaCreator.CreateSchema(workspace, enumNameMapping);
    }
}
[VB.NET]
Private Sub SchemaCreationExample(ByVal workspace As IWorkspace)
    ' Create a new fields collection.
    Dim fieldsEdit As IFieldsEdit = New FieldsClass
    
    ' Create an Object ID field.
    Dim oidFieldEdit As IFieldEdit = New FieldClass
    oidFieldEdit.Name_2 = "OBJECTID"
    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID
    fieldsEdit.AddField(oidFieldEdit)
    
    ' Create a text field.
    Dim textFieldEdit As IFieldEdit = New FieldClass
    textFieldEdit.Name_2 = "NAME"
    textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString
    textFieldEdit.Length_2 = 100
    fieldsEdit.AddField(textFieldEdit)
    
    ' Create a date field.
    Dim dateFieldEdit As IFieldEdit = New FieldClass
    dateFieldEdit.Name_2 = "BIRTHDATE"
    dateFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate
    fieldsEdit.AddField(dateFieldEdit)
    
    ' Create the table data element and apply the fields collection.
    Dim deTable As IDETable = New DETableClass
    deTable.OIDFieldName = "OBJECTID"
    deTable.HasOID = True
    deTable.Fields = fieldsEdit
    
    ' Set the DETable's data element properties.
    Dim dataElement As IDataElement = CType(deTable, IDataElement)
    dataElement.Name = "PEOPLE"
    dataElement.FullPropsRetrieved = True
    dataElement.CatalogPath = "/OC=PEOPLE"
    
    ' Set the DETable's GDB data element properties. Use a class description
    ' to get the table's instance CLSID.
    Dim deGdbTable As IDEGdbTable = CType(deTable, IDEGdbTable)
    Dim objClassDesc As IObjectClassDescription = New ObjectClassDescriptionClass
    Dim objectUID As UID = objClassDesc.InstanceCLSID
    deGdbTable.CLSID = Convert.ToString(objectUID.Value)
    
    ' Create an ESRI Array to contain the dataset data element or elements.
    Dim dataElementsArray As IArray = New ArrayClass
    dataElementsArray.Add(deTable)
    
    ' Instantiate a new schema creator and generate an enumerator of name mappings.
    Dim gdbSchemaCreator As IGdbSchemaCreator = New GdbSchemaCreatorClass
    Dim enumNameMapping As IEnumNameMapping = Nothing
    Dim conflictsFound As Boolean = False
    gdbSchemaCreator.GenerateNameMapping(workspace, dataElementsArray, Nothing, enumNameMapping, conflictsFound)
    
    ' If conflicts were not found, generate the schema.
    If Not conflictsFound Then
        gdbSchemaCreator.CreateSchema(workspace, enumNameMapping)
    End If
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update