markthewizard1234
markthewizard1234

Reputation: 443

Create A View, Select a Column As Primary Key

I have a long code line to create a view. For Entity framework to work with this view I need a primary key.

How can I select the column as a primary key?

I've tried this;

dstchannel,billsec,disposition,accountcode, uniqueid as PRIMARY KEY,cnam, callRateImport.Description, callRateImport.Code, callRateImport.Day FROM callLogImport
LEFT JOIN callRateImport ON 

uniqueid as PRIMARY KEY.

The other method I tried (to alter view and create primary key) works, but it has to be done before the view is created..

Upvotes: 2

Views: 5500

Answers (1)

IAmGroot
IAmGroot

Reputation: 13865

You cannot create primary keys on the view itself.

To get around the no primary key issue, you can create a unique column that the entity framework will pick up and use.

ISNULL(CAST((row_number() OVER (ORDER BY <columnName>)) AS int), 0) AS ID

So:

 SELECT  ISNULL(CAST((row_number() OVER (ORDER BY uniqueid)) AS int), 0) AS ID,  dstchannel,billsec,disposition,accountcode, uniqueid,cnam, callRateImport.Description, callRateImport.Code, callRateImport.Day FROM callLogImport
 LEFT JOIN callRateImport ON ....

Upvotes: 2

Related Questions