Entity Framework and SQL views

entity-framework sql view


I want to include a SQL view into my program. I've been working with Entity Framework so far. The issue, however, is that Entity Framework begins considering my view as a table when I add a view to it.

I truly want to know: Is there anything I'm overlooking? Will this issue also be fixed if I use Nhibernate? Will it just treat the view as that?

This view is an extremely intricate query that uses aggregation and several joins. I am utilizing a view because of this.

5/26/2012 8:15:21 PM

Popular Answer

But the problem is that when I add a view to Entity Framework it starts treating my view as a table.

Not at all, no. When you add a view to your model using the wizard (EDMX designer), the view is handled internally as a defining query, creating a read-only object. There is no difference at the entity level (the conceptual model) since it is simply another entity or class, but if you attempt to alter an instance of that class and save your changes, an exception will be raised (unless you map stored procedures or custom SQL commands to insert, update and delete operations for that entity).


Database first scenarios only use database views and other database-specific capabilities like stored procedures or SQL functions (when you are using model update using a database in the designer).

When telling VS, "Here is my model and I want some database to store it," you should use create a database using the model. First, only data from the conceptual model is utilized (original mapping and database description is replaced with a new one every time you run this command so even mapping to original database can be broken). It is unable to generate database-specific features for you since it is unaware of how to construct a view and how to map a class to a view (the query from original view is unknown).

Although you may have Visual Studio construct the view for you, doing so with T4 templates requires a lot of effort since you must somehow give a SQL generation script for the view.

5/28/2012 12:37:21 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow