3

我有以下代码

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);
            }
        }
    }
}

我得到空值而不是模式名称。我如何从实体框架中获取 shemas 的名称。(在我的数据库中,我有两种不同的 shemas。)也许有人知道另一种方式?

4

2 回答 2

2

我有错误的方法。下面的代码显示了我如何能够获得方案的名称。

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();   
    }
}
于 2013-09-04T08:05:35.137 回答
1

尝试这个。它经过了很好的测试。如果有任何疑问,请告诉我。

using (var context = new WdmEntities())
                {
                    //get object models from context
                    var objContext = ((IObjectContextAdapter)context).ObjectContext;
                    var items = objContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace);
                    var container = items != null ? items.First() : null;
                    var schemas = new List<string>();
                    if(container != null)
                        schemas = container.BaseEntitySets.Select(set => set.Name).ToList();
                }
于 2013-09-03T11:43:18.180 回答