I
I
isxaker2013-09-03 13:17:07
.NET
isxaker, 2013-09-03 13:17:07

Get schema name for each entity object?

I have the following code

using (WdmEntities context = new WdmEntities())
{
    //get object models from context
    ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
    var container = objContext.MetadataWorkspace.GetEntityContainer(objContext.DefaultContainerName, DataSpace.CSpace);
    List<string> schemas = new List<string>();
    foreach (var set in container.BaseEntitySets)
    {
        foreach (var metaproperty in set.MetadataProperties)
        {
            //here 
            if(metaproperty.Name == "Schema")
            {
                //but metaproperty.Value == NULL
                schemas.Add(metaproperty.Value);
            }
        }
    }
}

I want to get the name of the database schema for each entity object. Instead, I get null values. Tell me what I'm doing wrong or any other way.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
isxaker, 2013-09-04
@isxaker

This is how I managed to get the schema name

using (WdmEntities context = new WdmEntities())
{
    //get object models from context
    ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
    //get all full names types from object model
    var fullNameTypes = objContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace);
    ///////////////////
    var conStr = objContext.Connection.ConnectionString;
    var connection = new EntityConnection(conStr);
    var workspace = connection.GetMetadataWorkspace();

    var entitySets = workspace.GetItems<EntityContainer>(DataSpace.SSpace).First().BaseEntitySets;
                
    for (int i = 0; i < fullNameTypes.Count; i++)
    {
        Type type = Type.GetType(fullNameTypes[i].FullName);
        string schema = entitySets[type.Name].MetadataProperties["Schema"].Value.ToString();   
    }
}

M
mayorovp, 2013-09-03
@mayorovp

Database schemas are not in CSpace, but in SSpace.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question