Updatable Views in Entity Framework 5/6

puddinman13 picture puddinman13 · Sep 24, 2013 · Viewed 11.1k times · Source

I have several Views that are updatable according to http://technet.microsoft.com/en-us/library/ms187956.aspx.

All of my views follow the specifications in the afformentioned article. I have verified in SQL Management Studio that the views can be updated, inserted to, and deleted from.

The research I have done has led me to two options to make the views in my Entity Framework 5/6 Model updatable:

  1. Remove the tag from each view, however, any work done in MyContext.edmx is overwritten when updating the context from the database. This means that this solution isn't very viable for my project.

  2. Adding a insert, update, and delete stored procedure for each view and mapping these in the designer. I don't particularly like the idea of having to create this many stored procedures.

Is there any easy way to tell EF5 OR EF6 that the views can be added to/updated/deleted from that will not be wiped out when running subsequent "Update Model from Database" commands without writing stored procedures for each entry method(insert, update, delete) on each view?

Answer

JamesWHurst picture JamesWHurst · Apr 13, 2014

I would think that your easiest method, would be to change the definition of your EntitySet in your StorageModel to tell it to regard it as a table, as opposed to a database-view.

Looking at the XML definition, where it says

<EntitySet Name="Products" store:Type="Views" ..

you change that to

<EntitySet Name="Products" store:Type="Tables" ..

(Note the "Products" is just an example) This should be in your .edmx file.
See pg 44, Lerman, "Programming Entity Framework", 2nd Ed.

Hope this helps.