如何从实体框架获取架构名称? [英] How to get name of schema from entity framework?
本文介绍了如何从实体框架获取架构名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码
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.)也许有人知道另一种方式?
I get null values instead of schemas names. How i can get names of shemas from entity framework. (In my db i have two different shemas.) Maybe someone knows another way?
推荐答案
我的方法错误.下面的代码显示了我如何获得方案的名称.
I had the wrong approach. The code bellow shows how i was able to get the name of the schemes.
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();
}
}
这篇关于如何从实体框架获取架构名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文