45

I am getting a couple of these errors that make perfect sense as they are on views. I understand what they mean, however I am looking for a way to prevent that warning message from being generated with the model. I thought I could edit the .edmx XML to remove the errors but the warnings get regenerated. I have a key defined on the views although it doesn't seem to have helped.

Any way to get rid of these warnings? Or is there someway to have the Entity Framework realize that this is not an editable table and that is doesn't need a primary key? I'm asking mostly from a project aesthetics point-of-view (I don't like seeing warnings in my Error List).

test
  • 2,432
  • 2
  • 22
  • 48

5 Answers5

39

I perfectly understand where you're coming from. This is one of the biggest annoyances I've had to deal with when trying to get to grips with the EF and, apparently, it's a very common issue.

The solution that worked for me was to open the EDMX with the view in designer mode and assign a primary key myself. It doesn't matter really what it is, as long as there is one. You right-click on the column you want to set as primary key and select Entity Key (it's a checkbox option). You may have to exit and restart Visual Studio until this warning decides to go away.

I hope it works for you too. But even if it does, it's a short-term fix, as the next time you update your view, it will probably have the same problem. But as long as the above steps work, it should take just seconds to fix.

Also, here's some links you might find useful:

Community
  • 1
  • 1
djikay
  • 9,792
  • 8
  • 40
  • 50
  • 8
    You are not completely right. You cannot mark any column you want because if you mark a column that does not have unique values, you will get a bunch of run-time errors telling you that you have two object with the same primary/entity key. – Santhos Sep 27 '14 at 12:51
  • So that worked for me, moving the *suggested* Primery Key on my view using the right-click-on-column in the .edmx window. Restarted and it kept it, thank you very much. Time will tell however with this mad scientist stuff. – JustJohn Oct 14 '14 at 20:58
  • 5
    This doesn't work. I have a manually defined primary key on my view entity, on a unique column (which happens to be a primary key on the underlying table) and I still get this warning. – Tsahi Asher Oct 07 '15 at 11:46
  • 2
    @Santhos ... or even nastier, if you're using the entity as a read-only entity, you can get duplicates of entities because they've been cached intra-query, EF sees the "same key" pop up later in the same query, and then dupes the original. /sigh Fun times. – ruffin Mar 22 '16 at 21:26
  • 1
    @ruffin That is rough (no pun intended). I have never encountered that behaviour, but it sounds like a long night debugging session. – Santhos Mar 23 '16 at 07:36
  • Most likely it can be fixed by just closing and re-opening the project, as described below by @HiteshMistry. – Jgreenst Nov 30 '17 at 18:05
30

In my case this has solved easily, below are steps:

  • Clean up the solution
  • Close Visual Studio
  • Open Visual Studio
  • Clean up and Build the solution

Before trying anything, this can be tried to make sure you are not changing anything manually in edmx.

I hope this will help.

Hitesh
  • 2,377
  • 16
  • 24
6

I have had luck in EF4 and EF5 using the technique described by http://www.hilmiaric.com/?p=95 using rownumber as the ID

SELECT ISNULL((ROW_NUMBER() OVER (ORDER BY XXX ASC)), 0) AS 'ID',

But I don't have a solution for EF5 and above. Last tried on 6.1.3

Community
  • 1
  • 1
Keith H.
  • 318
  • 3
  • 5
0

Entity Framework can be so frustrating at times. I really wish that Microsoft would get off its behind and fix all the problems with the EF Visual Modeler and updating the model from database. I just spent the last half hour cleaning up duplicate fields in a view through manual modification of the .edmx file, and I also encountered a bad build and this problem once again:

 <!--Errors Found During Generation:
  warning 6002: The table/view 'BMP_DBA.BMP_INST_DATA_SEARCH_VIEW' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
 -->

Alas, I had to do like I have always done, and manually set the Primary key by right clicking on it in the designer, and then restarting Visual Studio and then rebuilding the app. When I rebuild I still get the warning in the .edmx file but the build now works. This is just one of the nuances of dealing with EF.

David Spenard
  • 731
  • 7
  • 9
  • 4
    are you sure this is an answer? – techspider Jul 12 '16 at 20:02
  • Manually adding a primary key on the view is one of the solutions to the problem, even if a primary key is not needed, just as the original question stated. – David Spenard Jul 12 '16 at 21:29
  • You don't need to clean up duplicate fields in a view by manually editing the edmx. Just remove the view from the designer and update the model and re-add the view. – LarryBud Sep 12 '18 at 14:31
0

Check the database table for primary key. Maybe the ID column marked as identity but not as primary key.

yikekas
  • 91
  • 1
  • 5