Entity Framework and SQL Server View
I was able to resolve this using the designer.
- Open the Model Browser.
- Find the view in the diagram.
- Right click on the primary key, and make sure "Entity Key" is checked.
- Multi-select all the non-primary keys. Use Ctrl or Shift keys.
- In the Properties window (press F4 if needed to see it), change the "Entity Key" drop-down to False.
- Save changes.
- Close Visual Studio and re-open it. I am using Visual Studio 2013 with EF 6 and I had to do this to get the warnings to go away.
I did not have to change my view to use the ISNULL, NULLIF, or COALESCE workarounds. If you update your model from the database, the warnings will re-appear, but will go away if you close and re-open VS. The changes you made in the designer will be preserved and not affected by the refresh.
We had the same problem and this is the solution:
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.
Example:
SELECT
ISNULL(MyPrimaryID,-999) MyPrimaryID,
NULLIF(AnotherProperty,'') AnotherProperty
FROM ( ... ) AS temp