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:
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.
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?
To use view as an entity, first you will need to add database views to EDM. After adding views to your model then you can work with it the same way as normal entities except for Create, Update, and Delete operations. Let's take a look, how to add views into the model from the database.
A deletable view becomes an updatable view when at least one of its columns is updatable. A column of a view is updatable when all of the following rules are true: The view is deletable. The column resolves to a column of a table (not using a dereference operation) and the READ ONLY option is not specified.
In this situation you can upgrade to EF5 using the following steps: Select Tools -> Library Package Manager -> Package Manager Console. Run Install-Package EntityFramework -version 5.0.
The Entity Framework (EF) is an Object Relational Mapping (ORM) tool that allows developers to work with the database by simply writing . NET code. For an introduction to EF take a look at our earlier tip Intro to Entity Framework with SQL Server. EF has built-in support for using existing views.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With