0

I am using EF 6.1 and link to entities (OData). I need to get the name of the Primary Key column(s) in a generic fashion.

I thought I had found the answer here

Using this bit of code:

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

The code runs fine, but does not return any values because none of the property attributes is of type EdmScalarPropertyAtttribute.

Everything is a System.CodeDom.Compiler.GeneratedCodeAttribute. I cannot find anything in the GetCustomAttributes or other methods and properties that would help me find the PK.

How can I find the PK of a table in EF 6.1?

Community
  • 1
  • 1
Mark Bonafe
  • 1,461
  • 13
  • 23
  • [Entity Framework Code First Find Primary Key](http://stackoverflow.com/questions/7253943/entity-framework-code-first-find-primary-key) – Yuliam Chandra Aug 14 '14 at 14:50
  • Thanks, Yuliam, but my context is a System.Data.Services.Client.DataServiceContext. It cannot be converted to an IObjectContextAdapter. – Mark Bonafe Aug 15 '14 at 16:13

0 Answers0