Brains Coders

Brains Engineering Blog

Views and Entity Framework: missing primary key

if you are trying to map a view in the entity framework and you get the following message:

"The table/view TABLE_NAME does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it"

that means that you don't have selected a primary key from source table(s) in your view.

If you can't add it, for example as you're making a distinct aggregation operation, using RowNumber(), you use the command ISNULL():

SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY StatsSum.SvyDate, StatsSum.Adm0Name),-1) ID, ....

ISNULL takes two arguments: the key field, and what it should return if there's a null value (-1 in the above example).