View from MSSQL in Entity Framework without primary key

Often we need to import to the Entity Framework an SQL View… and many times

the SQL View could be the result of UNION commands, GROUP, etc.

Entity Framework needs on every object a primary key (or similar) to use it.

So you can use the following rules :

To force entity framework to use a column as a primary key, use ISNULL.

To force entity framework not to use a column as a primary key, use NULLIF.

An easy way to apply this is to wrap the select statement of your view in another select.



ISNULL(MyPrimaryID,-999) MyPrimaryID,

NULLIF(AnotherProperty,”) AnotherProperty

FROM ( … ) AS temp

Potrebbe interessanti anche…