using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;
class BrowseTypes
{
static void Main()
{
try
{
// Establish a connection to the underlying data provider by
// using the connection string specified in the config file.
using (EntityConnection connection =
new EntityConnection("Name=AdventureWorksEntities"))
{
// Open the connection.
connection.Open();
// Access the metadata workspace.
MetadataWorkspace workspace =
connection.GetMetadataWorkspace();
// Browse the metadata type hierarchy in the conceptual model.
BrowseTypesExample(workspace, DataSpace.CSpace);
// Browse the metadata type hierarchy in the storage model.
BrowseTypesExample(workspace, DataSpace.SSpace);
}
}
catch (MetadataException exceptionMetadata)
{
Console.WriteLine("MetadataException: {0}",
exceptionMetadata.Message);
}
catch (System.Data.MappingException exceptionMapping)
{
Console.WriteLine("MappingException: {0}",
exceptionMapping.Message);
}
}
private static void BrowseTypesExample(MetadataWorkspace workspace,
DataSpace model)
{
// Get a collection of the GlobalItems.
// An GlobalItem class is the base class for
// the entity data model types and entity containers.
ReadOnlyCollection<GlobalItem> items =
workspace.GetItems<GlobalItem>(model);
// Iterate through the collection to get each item.
foreach (GlobalItem item in items)
{
EntityContainer entityContainer = item as EntityContainer;
if (entityContainer != null)
{
Console.WriteLine(
"EntityContainer Name: {0}",
entityContainer.Name);
continue;
}
EntityType entityType = item as EntityType;
if (entityType != null)
{
Console.WriteLine(
"EntityType Name: {0}, Namespace: {1}",
entityType.Name, entityType.NamespaceName);
continue;
}
AssociationType associationType = item as AssociationType;
if (associationType != null)
{
Console.WriteLine(
"AssociationType Name: {0}, Namespace: {1}",
associationType.Name, associationType.NamespaceName);
continue;
}
PrimitiveType primType = item as PrimitiveType;
if (primType != null)
{
Console.WriteLine(
"PrimitiveType Name: {0}, Namespace: {1}",
primType.Name, primType.NamespaceName);
continue;
}
EdmFunction function = item as EdmFunction;
if (function != null)
{
Console.WriteLine(
"Function Name: {0}, Namespace: {1}",
function.Name, function.NamespaceName);
continue;
}
}
}
} |