Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updatable Views in Entity Framework 5/6

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?

like image 226
puddinman13 Avatar asked Sep 24 '13 21:09

puddinman13


People also ask

How do I update Entity Framework 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.

Which views are updatable?

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.

How can I change Entity Framework 6 to 5?

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.

Does Entity Framework support views?

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.


1 Answers

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.

like image 114
JamesWHurst Avatar answered Sep 23 '22 14:09

JamesWHurst