实体框架的SQL视图 [英] SQL views with Entity Framework

查看:132
本文介绍了实体框架的SQL视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL视图与我的应用程序集成。我一直在使用实体框架。但问题是当我添加一个视图到Entity框架它开始把我的视图作为一个表。

I have a SQL view to integrate with my application. I have been using Entity Framework till now. But the problem is that when I add a view to Entity Framework it starts treating my view as a table.

我真正想知道的是,我错过了一些东西?另外如果我使用Nhibernate会这个问题解决吗?将视图视为仅视图?

What I really want to know is, am I missing on something? Also if I use Nhibernate will this problem be resolved? Will it treat the view as a view only?

此视图是一个非常复杂的查询,具有多个连接和聚合。这就是为什么我使用一个视图。

This view is a very complex query which has multiple joins and aggregation. That is why I am using a view.

推荐答案


但问题是,当我添加查看实体框架它
开始将我的视图视为一个表。

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

不,没有。如果您通过向导(EDMX设计器)向模型添加视图,则它将内部将视图视为定义查询,该查询使readonly实体。在实体级别(概念模型),您没有看到差异,因为它只是另一个实体/类,但是如果您尝试对该类的实例进行更改并保存它们,则会收到异常(除非映射存储过程或自定义SQL命令来插入,更新和删除该实体的操作。)

No it doesn't. If you add view to your model through wizard (EDMX designer) it will internally handle the view as a defining query which makes readonly entity. At entity level (the conceptual model) you don't see a difference because it is just another entity / class but if you try to make changes to instance of that class and save them you will get an exception (unless you map stored procedures or custom SQL commands to insert, update and delete operations for that entity).

编辑:

数据库视图为以及其他数据库特定功能(如存储过程或SQL函数)仅适用于数据库第一种情况(当您在设计器中使用从数据库更新模型时)。

Database views as well as other database specific features like stored procedures or SQL functions are only for database first scenario (when you are using Update model from database in the designer).

使用从模型生成数据库是针对Model第一种情况,您告诉VS:这是我的模型,我想要一些数据库来存储。首先,仅使用概念模型中的信息(每次运行此命令时,原始映射和数据库描述都被替换为新的,所以即使映射到原始数据库也可能会被破坏)。它不能为您创建数据库特定的功能,因为它不知道该类应该映射到视图,而且它不知道应该如何创建视图(来自原始视图的查询是未知的)。

Using Generate database from model is for Model first scenario where you tell VS: "Here is my model and I want some database to store it." First of all only information from conceptual model is used (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 cannot create database specific features for you because it doesn't know that class should be mapped to view and moreover it doesn't know how should the view be created (the query from original view is unknown).

您可以强制VS为您创建视图,但在T4模板中有很多工作,您将不得不为视图提供SQL创建脚本。

You can force VS to create the view for you but it is a lot of work in T4 templates where you will have to somehow provide SQL creation script for the view.

这篇关于实体框架的SQL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆