locked
POCO entity from a view RRS feed

  • Question

  • Hi all,

    In our database we have a view that points to different database. However we need to treat this view as an normal 'table' entity in EF so that we can perform CRUD operations.

    When validating the edmx, the following message is show:

    The table/view 'dbo.postcodes' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.

    Can we just ignore the message and treat the generated POCO class normally?

    Thanks,

    Ivan

    Thursday, May 10, 2012 11:55 AM

Answers

  • Hi Cryo75,

    Firstly, you need to make sure that the view is updatable.

    If so, after importing the view into the model, edit the edmx file with a xml editor, locate in the StorageModel the entity set for the view and change the following items:

    store:type - change from view to table
    defining query - remove it
    store:name - remove it 

    In this way, EF will treate it as a table by that name and it will try to use it as a table to perform insert/update/delete operations. If your view is updatable, then it should work.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Friday, May 11, 2012 6:40 AM
    • Unmarked as answer by Cryo75 Friday, May 11, 2012 7:15 AM
    • Marked as answer by Cryo75 Friday, May 11, 2012 7:55 AM
    Friday, May 11, 2012 2:59 AM

All replies

  • Hi Cryo75,

    Firstly, you need to make sure that the view is updatable.

    If so, after importing the view into the model, edit the edmx file with a xml editor, locate in the StorageModel the entity set for the view and change the following items:

    store:type - change from view to table
    defining query - remove it
    store:name - remove it 

    In this way, EF will treate it as a table by that name and it will try to use it as a table to perform insert/update/delete operations. If your view is updatable, then it should work.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Friday, May 11, 2012 6:40 AM
    • Unmarked as answer by Cryo75 Friday, May 11, 2012 7:15 AM
    • Marked as answer by Cryo75 Friday, May 11, 2012 7:55 AM
    Friday, May 11, 2012 2:59 AM
  • It works only partially. When I add new tables or refresh existing tables using the designer, I lose the mappings to these views and I get errors on validation.

    Friday, May 11, 2012 7:16 AM
  • Hi Cryo75,

    Yes, everytime you update the model from database, the modification of the edmx file will gone, and you need to reapply the steps. But it's the only way to create an updatable view in Entity Framework. Please refer to this link.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, May 11, 2012 7:52 AM