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

前端 未结 5 1873
刺人心
刺人心 2020-12-02 22:27

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 messa

相关标签:
5条回答
  • 2020-12-02 23:04

    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

    0 讨论(0)
  • 2020-12-02 23:04

    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.

    0 讨论(0)
  • 2020-12-02 23:11

    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.

    0 讨论(0)
  • 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:

    • SO: Entity Framework and SQL Server View
    • SO: cannot add view to the edmx
    • ASP.NET forum: Entity model from table views.
    0 讨论(0)
  • 2020-12-02 23:21

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

    0 讨论(0)
提交回复
热议问题