entity framework – 如何获取列?

我希望获得列名称,类型以及列是否是Entity Framework中表对象的PK的列表。

我如何在C#(4.0)(理想情况下)中执行此操作?

获胜的答案将是有效且最重要的一般答案。

知道了 – 我使用了基于linq的reflection查询:

IEnumerable properties = from p in typeof(T).GetProperties() where (from a in p.GetCustomAttributes(false) where a is EdmScalarPropertyAttribute select true).FirstOrDefault() 

排序! 谢谢你的建议。

仅供参考 – 我正在使用LINQ创建动态where子句,动态lambda表达式构建例如搜索,默认情况下将自动搜索所有列。 但我还需要validation列名称,因为我将允许覆盖它,并且这些调用将通过javascript ajax post完成,其输入不可信任 – 因此需要validation列名称。

我使用上面的方法将结果放入一个名为FieldName,FieldType,PrimaryKey属性的自定义对象中。 Ta daaa。

用它进一步定制

 IEnumerable properties = from p in typeof(T).GetProperties() where (from a in p.GetCustomAttributes(false) where a is EdmScalarPropertyAttribute select true).FirstOrDefault() select new FieldList { FieldName = p.Name, FieldType = p.PropertyType, FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0 }; 

如果你只想要列名,我得到了最好的答案:
var properties = (from t in typeof(YourTableName).GetProperties() select t.Name).ToList(); var name= properties[0];

如果您不想使用reflection,请在此处查看答案。 将实体名称替换为您的实体名称

 var cols = from meta in ctx.MetadataWorkspace.GetItems(DataSpace.CSpace) .Where(m=> m.BuiltInTypeKind==BuiltInTypeKind.EntityType) from p in (meta as EntityType).Properties .Where(p => p.DeclaringType.Name == "EntityName") select new { PropertyName = p.Name, TypeUsageName = p.TypeUsage.EdmType.Name, //type name Documentation = p.Documentation != null ? p.Documentation.LongDescription : null //if primary key }; 

如果有人还在寻找,我就是这样做的。 这是DBContext的扩展方法,它接受类型并返回物理列名称及其属性。

这利用对象上下文获取物理列列表,然后使用“PreferredName”元数据属性将每个列映射到其属性。

由于它使用对象上下文,因此它会启动数据库连接,因此第一次运行将会很慢,具体取决于上下文的复杂性。

 public static IDictionary GetTableColumns(this DbContext ctx, Type entityType) { ObjectContext octx = (ctx as IObjectContextAdapter).ObjectContext; EntityType storageEntityType = octx.MetadataWorkspace.GetItems(DataSpace.SSpace) .Where(x => x.BuiltInTypeKind == BuiltInTypeKind.EntityType).OfType() .Single(x => x.Name == entityType.Name); var columnNames = storageEntityType.Properties.ToDictionary(x => x.Name, y => y.MetadataProperties.FirstOrDefault(x => x.Name == "PreferredName")?.Value as string ?? y.Name); return storageEntityType.Properties.Select((elm, index) => new {elm.Name, Property = entityType.GetProperty(columnNames[elm.Name])}) .ToDictionary(x => x.Name, x => x.Property); } 

要使用它,只需创建一个辅助静态类,并添加上面的函数; 然后就像打电话一样简单

 var tabCols = context.GetTableColumns(typeof(EntityType)); 

我没有为您提供代码示例,但只是为了指向正确的方向,您可能需要考虑使用Sql管理对象(SMO); 您可以使用它来获取Sql Server实例的对象层次结构,然后您可以枚举并选择所需的信息。

看一下这套教程,让您开始编程 – http://www.codeproject.com/KB/database/SMO_Tutorial_1.aspx http://www.codeproject.com/KB/database/SMO_Tutorial_2.aspx

如果您使用的是DB First或Model First,请打开在文本编辑器中生成的.edmx文件EF。 它只是一个XML文件,它包含您需要的一切。 这是我的模型的一个例子。 请注意,我使用的是Oracle的EF驱动程序,所以你的看起来不一样(但它应该非常接近)。

         
           <密钥>
             
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
           
         

您可以使用XML解析器来解析文件并获得所需内容。 .edmx文件包含有关实体和SQL表的数据,因此您需要确保获得正确的部分以获得所需内容。

 typeof(TableName).GetProperties().Select(x => x.Name).ToList();