EF获取数据库表名和列名
新建模板⼩书匠
/// <summary>
/// 通过当前DBContext上下⽂获取对应数据库中所有得表
/// </summary>煤矿合同
/// <returns></returns>
public List<string> GetTableNames()
{
var TableNames = new List<string>();
var metadata = ((IObjectContextAdapter) this).ObjectContext.MetadataWorkspace;
var tables = metadata.GetItemCollection(DataSpace.SSpace).GetItems<EntityContainer>().Single()
.BaseEntitySets.OfType<EntitySet>()
.Where(s => !s.MetadataProperties.Contains("Type") ||
s.MetadataProperties["Type"].ToString() == "Tables");
foreach (var table in tables)
{
var tableName = table.MetadataProperties.Contains("Table") &&
table.MetadataProperties["Table"].Value != null
table.MetadataProperties["Table"].Value.ToString()
吴敏霞个人资料: table.Name;
var tableSchema = table.MetadataProperties["Schema"].Value.ToString();
TableNames.Add(tableSchema + "." + tableName);
}
return TableNames;
}
public List<string> GetColumnNamesByTable(string tableName)
{
string name = tableName.Split('.')[1];
电子表格打不开怎么办string schema = tableName.Split('.')[0];
六爻入门var ColumnNames = new List<string>();
var metadata = ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace;
var table = metadata.GetItemCollection(DataSpace.SSpace).GetItems<EntityContainer>().Single().B
aseEntitySets.OfType<EntitySet>().Where(s=>!s.MetadataProperties.Co ntains("Type") || s.MetadataProperties["Type"].ToString() == "Tables").FirstOrDefault(t => t.Table==name&&t.Schema==schema);
foreach (var member in table.ElementType.Members)
生化危机2 重制{
var ColumnName = member.Name;
ColumnNames.Add(ColumnName);
}
return ColumnNames;
孙俪身高}
发布评论