Error 6002: The table/view does not have a primary key defined
Asked Answered
T

5

49

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).

Triclinic answered 16/7, 2014 at 23:0 Comment(0)
C
42

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:

Castorena answered 16/7, 2014 at 23:28 Comment(7)
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.Tanhya
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.Botany
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.Naphtha
@Tanhya ... 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.Liquorice
@Liquorice That is rough (no pun intended). I have never encountered that behaviour, but it sounds like a long night debugging session.Tanhya
Most likely it can be fixed by just closing and re-opening the project, as described below by @HiteshMistry.Turcotte
Note that setting an entity key does not set a primary key. The two are not the same thing. Views do not have primary keys. Entities have entity keysDogs
S
34

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.

Suited answered 4/8, 2016 at 7:4 Comment(2)
that awkward moment you double click the .edmx file after doing this.Blandishment
Unfortunately, evading the edmx warnings does not resolve or ignore them. Once you open the edmx they are still there. I would not call that a resolution/solving.Dogs
S
7

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

Seise answered 16/4, 2016 at 21:12 Comment(2)
Dead link. Doesn't this cause the same issue when you open the edmx? row_number returns a bigint, which is still a scalar type, and not a PK on a view. So generating the entity would mark it as an entity type, and produce the warning when doing so.Dogs
Note also that entities are tracked by this ID, and subsequent queries will reuse an entity cache. I assume if this view is queried twice and the data or order of the data is not consistent you will get mismatched/inconsistent results. Faking a unique identity when it is not can have unforseen and hard to spot issues later.Dogs
C
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.

Cutshall answered 12/7, 2016 at 19:36 Comment(3)
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.Cutshall
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.Swashbuckler
Note that setting an entity key does not set a primary key. The two are not the same thing. Views do not have primary keys. Entities have entity keys.Dogs
B
-1

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

Bizarre answered 10/7, 2020 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.