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?