How do you add a primary key to a sql view? - Or alternate way to link views to LINQ-2-Entities

.net, linq-to-entities, sql-server, t-sql

Solution

After you create the view using `schemabinding` you can add a primary key to it:

CREATE VIEW Colors WITH SCHEMABINDING
AS SELECT Color='yellow', ColorCount=100
GO
CREATE UNIQUE CLUSTERED INDEX PK_Colors ON Colors (Color)

Problem

I'm adding a very simple view (or trying to) to my entities object model. The database is in SQL Server 2008. I'm on .Net 3.5 (SP1) using C#. The view has two fields: color and colorcount, a Varchar(50) and a count(*) respectively. When I do Update Model from Database, and select the view to add, it runs (it updated tables, adding fields no problem) but does not add the view. No error, warnings, or messages are displayed. When I open the .edmx file I see that it shows `Warning 6013: No primary key defined.` The view is complex and I would rather not translate it to a LINQ query. How can I add a primary key so that Entities will support the view? Is there a non-hack-around way to add a view like this to an EDMX?

Original source