locked
From a DB perspective, views do NOT always need a primary key RRS feed

  • General discussion

  • All,

    I'm sure this has been mentioned before, but when I create a view in my DB, it's usually because I'm only interested in certain columns and usually only for reporting (read-only) purposes.  In other words, my view does NOT need a primary key (strictly from a DB perspective).

    Then comes Entity Framework, and every time I try to import one of these views, if a PK cannot be inferred it does not allow me to import.  I'm sure there's some really good technical reason for this, but for future versions of EF, it would help developers out a lot (at least me) if we had the option to import read-only views and not have EF complain about no primary keys.  Yes, I know it has no PK.  That's why it's a VIEW and not a TABLE.

    Maybe going forward, we can have the option when we update our model from the DataBase to toggle "read-only" for certain views so EF doesn't think it'll have to write anything, then it won't complain about primary keys for those read-only views.  Otherwise, I'm stuck creating a B.S. column in my view for the purposes of primary-key, which is a pain.

    Friday, October 26, 2012 12:47 AM

All replies

  • Hi Whiteheadw,

    Welcome to the MSDN forum.

    Please try the suggestions in this page: http://stackoverflow.com/questions/1013333/entity-framework-and-sql-server-view-question

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 29, 2012 8:18 AM
  • Hi Alexander.  I already know about these cumbersome workarounds.  Clearly you missed the point of this thread.  The point I'm making (and I'm sure somewhere out there other developers would strongly agree) is that it would be really helpful to developers if future versions of EF had the option to import views as read-only.  The idea is that read-only views, since they wouldn't have to save anything, wouldn't need a PK, and therefore EF wouldn't insist on a PK for these read-only views.

    Monday, October 29, 2012 4:44 PM