views:

38

answers:

3

I get the following error message

The table/view 'TABLE1' 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

when I try to add a view to an Entity Data Model. For testing, I have created a very simple view and I still get the error. below is the view definition

SELECT 
   DISTINCT TOP (100) PERCENT 
   MIN(id) AS Expr4, MIN(EmpNo) AS Expr1, 
   MIN(Name) AS Expr2, MIN(Category) AS Expr3
FROM  dbo.MYView1
GROUP BY id

does any one know of a simple work around.

I have looked at this thread http://stackoverflow.com/questions/745341/can-ms-sql-views-have-primary-and-foriegn-keys but in my scenario, what is recommended is not applicable.

If I could only have the view added to the Model.

A: 

Set a primary key for the table.

gmcalab
"id" is the tables primary key
Name.IsNullOrEmpty
Um, no, it's a unique column, which is not the same as a PK.
Craig Stuntz
A: 

According to Social MSDN this is caused by an Entity Framework bug.

amelvin
No, not really.
Craig Stuntz
A: 

Since your view doesn't have a primary key constraint, you need to help the EF out a little. You do this by editing the CSDL. You only need to do the first part if you don't intend to update.

Craig Stuntz
thanks. this works for me.
Name.IsNullOrEmpty