1 out of 2 rated this helpful - Rate this topic

EntityContainer Class

Represents an entity container in a conceptual model. An EntityContainer is a logical grouping of entity sets and association sets.

System.Object
  System.Data.Metadata.Edm.MetadataItem
    System.Data.Metadata.Edm.GlobalItem
      System.Data.Metadata.Edm.EntityContainer

Namespace:  System.Data.Metadata.Edm
Assembly:  System.Data.Entity (in System.Data.Entity.dll)
public sealed class EntityContainer : GlobalItem

The EntityContainer type exposes the following members.

  Name Description
Public property BaseEntitySets Gets a list of entity sets and association sets that this EntityContainer includes.
Public property BuiltInTypeKind Gets the built-in type kind for this EntityContainer. (Overrides MetadataItem.BuiltInTypeKind.)
Public property Documentation Gets or sets the documentation associated with this type. (Inherited from MetadataItem.)
Public property FunctionImports Specifies a collection of EdmFunction elements. Each function contains the details of a stored procedure that exists in the database or equivalent CommandText that is mapped to an entity and its properties.
Public property MetadataProperties Gets the list of properties of the current type. (Inherited from MetadataItem.)
Public property Name Gets the name of this EntityContainer.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEntitySetByName Returns an EntitySet object by using the specified name for the entity set.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetRelationshipSetByName Returns a RelationshipSet object by using the specified name for the relationship set.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns the name of this EntityContainer. (Overrides Object.ToString().)
Public method TryGetEntitySetByName Returns an EntitySet object by using the specified name for the entity set.
Public method TryGetRelationshipSetByName Returns a RelationshipSet object by using the specified name for the relationship set.
Top

On the conceptual level, the EntityContainer class represents a container that will be mapped to a database object in the storage metadata schema. In the storage level, the EntityContainer class represents a description of the table and/or key relationships that persist data for applications built on the model. For more information about the entity containers in a conceptual model, see Entity Containers (EDM).

The following code sample demonstrates how to get a metadata workspace from the connection and then use that metadata workspace to retrieve information about the entity containers in the specified data model. Note that the metadata workspace is a runtime service component that provides support for retrieving metadata.

The code sample uses a CSpace and a SSpace to specify the model. The CSpace represents the default name for the conceptual model. The SSpace represents the default name for the storage model.

The GetEntityContainers method gets a collection of entity containers and then iterates through the collection to get each entity set and association set in the specified container. The code sample uses the AdventureWorks Model that is provided in the Adventure Works Model (EDM)topic. For an example of the application config file, see Adventure Works Object Model in Applications (EDM).

using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;

class GetEntityContainerExample
{
  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();

         // Get the entity containers in the conceptual model.
         GetEntityContainers(workspace, DataSpace.CSpace);

         // Get the entity containers in the storage model.
             GetEntityContainers(workspace, DataSpace.SSpace);
       }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
      Console.WriteLine("MappingException: {0}",
                       exceptionMapping.Message);
    }
  }

  public static void GetEntityContainers(
      MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of the entity containers.
    ReadOnlyCollection<EntityContainer> containers = 
         workspace.GetItems<EntityContainer>(model);

    // Iterate through the collection to get each entity container.
    foreach (EntityContainer container in containers)
    {
       Console.WriteLine("EntityContainer Name: {0} ", 
                        container.Name);

       // EntitySetBase is a super type for 
       // EntitySets and RelationshipSets. 
       // Iterate through the collection to get each EntitySetBase.
       foreach (EntitySetBase baseSet in container.BaseEntitySets)
       {
          // Check if this instance is an EntitySet.
          if (baseSet is EntitySet)
          {
             Console.WriteLine(
                "  EntitySet Name: {0} , EntityType Name: {1} ",
                baseSet.Name, baseSet.ElementType.FullName);
          }

         // RelationshipSet is a super type for AssociationSet.
         // Check if this instance is an AssociationSet.
         if (baseSet is AssociationSet)
         {
            Console.WriteLine(
               "  AssociationSet Name: {0} , " +
               "AssociationType Name: {1} ",
                baseSet.Name, baseSet.ElementType.FullName);

            // Get the AssociationSet.
            AssociationSet associationSet = 
                  baseSet as AssociationSet;

            // Iterate through the collection to get 
            // each AssociatedSetEnd.
            foreach (AssociationSetEnd end in 
               associationSet.AssociationSetEnds)
            {
               Console.WriteLine(
                  "   EntitySet Name: {0} , Name: {1} ",
                  end.EntitySet, end.Name);
            }
         }
      }
    }
  }
}

.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ