C# 实体框架6-一般获取PK列

C# 实体框架6-一般获取PK列,c#,entity-framework,generics,C#,Entity Framework,Generics,我正在使用EF 6.1并链接到实体OData。我需要以通用方式获取主键列的名称 我以为我找到了答案 使用这段代码: IEnumerable<FieldList> properties = from p in typeof(T).GetProperties() where (from a in p.GetCustomAttributes(false) wher

我正在使用EF 6.1并链接到实体OData。我需要以通用方式获取主键列的名称

我以为我找到了答案

使用这段代码:

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
                                 };    
代码运行正常,但不返回任何值,因为所有属性属性都不是EdmscalArPropertyAttribute类型

一切都是System.CodeDom.Compiler.GeneratedCodeAttribute。我在GetCustomAttributes或其他方法和属性中找不到任何有助于我找到PK的内容


如何在EF 6.1中找到表的主键?

谢谢,Yuliam,但我的上下文是System.Data.Services.Client.DataServiceContext。无法将其转换为IObjectContextAdapter。