Example #1
0
        public static TableInfo GetTableInfo(this MetadataWorkspace metadata, Type entityType)
        {
            // Get the part of the model that contains info about the actual CLR types
            var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

            // Get the entity type from the model that maps to the CLR type
            var entityClrType = metadata
                                .GetItems <EntityType>(DataSpace.OSpace)
                                .Single(e => objectItemCollection.GetClrType(e) == entityType);

            // Get the entity set that uses this entity type
            var entitySet = metadata
                            .GetItems <EntityContainer>(DataSpace.CSpace)
                            .Single()
                            .EntitySets
                            .Single(s => s.ElementType.Name == entityClrType.Name);

            // Find the mapping between conceptual and storage model for this entity set
            var mapping = metadata.GetItems <EntityContainerMapping>(DataSpace.CSSpace)
                          .Single()
                          .EntitySetMappings
                          .Single(s => s.EntitySet == entitySet);

            // Find the storage entity set (table) that the entity is mapped
            var table = mapping
                        .EntityTypeMappings.Single()
                        .Fragments.Single()
                        .StoreEntitySet;

            // Return the table name from the storage entity set
            return(new TableInfo
            {
                Schema = (string)table.MetadataProperties["Schema"].Value,
                Name = (string)table.MetadataProperties["Table"].Value,
                ColumnPropertyMap = metadata.GetTableColumns(entityType),
                EFCoreName = entitySet.Name
            });
        }