实体框架 - 我怎么列? [英] Entity Framework - how do I get the columns?

查看:147
本文介绍了实体框架 - 我怎么列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望得到的列名,类型的列表,以及是否列在实体框架表对象的PK。

I wish to get a list of columns names, types and whether the column is a PK of a table object in Entity Framework.

我怎么做这在C#(4.0)(理想情况下统称)?

How do I do this in C# (4.0) (ideally generically)?

获奖的答案将是一个有效地做它,最重要的统称。

The winning answer will be one that does it efficiently and most importantly generically.

推荐答案

明白了 - 我使用基于LINQ查询反思:

Got it - I used a linq based reflection query:

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

排序!
感谢您的建议所有。

Sorted! Thanks for the suggestions all.

仅供参考 - 我创造一个充满活力的地方使用LINQ,拉姆达动态前pressions打造如条款搜索,它会自动默认通过所有列搜索。但我也需要列名来验证,因为我会允许这样的重写和这些呼叫将通过JavaScript AJAX后要做的输入不能被信任 - 所以需要验证列名

FYI - I am creating a dynamic where clause using LINQ, dynamic lambda expressions to build e.g. search which will automatically search through all columns by default. But I also needed the column names to verify because I will allow this to be overridden and these calls will be done via javascript ajax post whose input cannot be trusted - so needed to verify the column names.

我使用了上述的结果放到与性质称为字段名,的FieldType,PrimaryKey的自定义对象。钽daaa。

I used the above to place the results into a custom object with properties called FieldName, FieldType, PrimaryKey. Ta daaa.

进一步自定义它

IEnumerable<FieldList> 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
                                     };    

这篇关于实体框架 - 我怎么列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆