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

5 Answers 5

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:

7
  • 11
    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, 2014 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, 2014 at 20:58
  • 7
    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. Oct 7, 2015 at 11:46
  • 3
    @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, 2016 at 21:26
  • 2
    @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, 2016 at 7:36
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.

2
  • 2
    that awkward moment you double click the .edmx file after doing this.
    – Stavm
    Nov 12, 2019 at 9:23
  • 2
    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.
    – Kissaki
    Oct 25, 2021 at 14:54
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

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.
    – Kissaki
    Oct 25, 2021 at 14:59
  • 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.
    – Kissaki
    Oct 25, 2021 at 15:01
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.

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. Jul 12, 2016 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, 2018 at 14:31
  • 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.
    – Kissaki
    Oct 25, 2021 at 14:51
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.