数据库视图 (View)和EntityFramework Code First结合使用
问题来源:
项目需要进行查询操作,会涉及多表间的联合查询,使用Entity Framework会牵涉到效率问题,为增加查询速度,所以使用数据库视图技术,综合来讲是将Model类映射到数据库View。
来自MSDN的一篇文章Mapping to an Indexed View :
1) You can certainly use Code First to map to a view, just tell Code First that it's a table and it will use the same SQL against the view that it would for a table. Obviously if your view isn't writeable then saving is going to fail if you try and update values in the entities that are based on the view, but we will just delegate to the database so if you don't update these entities then you won't have any issues.
2) You don't need to have a foreign key in the database, Code First will assume you have one, but in this case that won't cause any issues because there will always be a matching customer for each row anyway. You will need to give Code First a valid primary key for the view, which sounds like it would be customer_id and currency_code in your case.
但该文章并未指出该如何实现Model类到已存在View的映射。
1、首先生成POCO实体类;
2、数据库底层访问实现,主要是OnModelCreating方法的书写
3、实现访问
此过程大致类似于Entity Framework Code First执行SQL语句、视图及存储过程,实现过程中仍会存在问题,关键点如下:
Key Point:生成POCO对象后,数据库会显示需要进行数据库升级,需要先将数据库升级,然后将新生成的Table删掉,生成视图与删掉的Table名称相同,此时POCO对象对应的就是视图View,由于视图的生成更新是由数据库自动完成,所以不能进行全部CRUD操作。
数据库视图 (View)和EntityFramework Code First结合使用的更多相关文章
- 数据库视图View的使用
一.视图的概念: 概念: 视图是指计算机数据库中的视图,是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据 ...
- MySQL数据库视图(view),视图定义、创建视图、修改视图
原文链接:https://blog.csdn.net/moxigandashu/article/details/63254901
- How to: Map a Persistent Class to a Database View Which Has No Key Field如何:映射持久化类到无主键数据库视图
With XAF, you can build new applications from scratch or maintain existing databases. The How to: Ge ...
- C# EntityFramework Code First 迁移 降级 回退到空数据库
C# EntityFramework Code First 迁移 降级 回退到空数据库 1.包管理器控制台-迁移 在包管理器控制台中运行 Enable-Migrations Add-Migratio ...
- MySQL/MariaDB数据库的视图(VIEW)
MySQL/MariaDB数据库的视图(VIEW) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.视图概述 1>.什么是视图 视图就是一个虚拟的表,保存有实表的查询结果 ...
- SQL Server 索引(index) 和 视图(view) 的简单介绍和操作
--索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...
- Entityframework Code First 系列之数据注释
上一篇<Entityframework Code First 系列之项目搭建>讲了搭建一个Code First的控制台项目.里面有一些内容并没有扩展出来讲,因为篇幅有限.这篇针对上面内容中 ...
- Oracle 学习笔记 11 -- 视图 (VIEW)
本次必须学习一个全新的概念-- 视图 (VIEW).在前面的笔记中曾提到过,数据对象包含:表.视图.序列.索引和同义词.前面的笔记都是对表的想剖析,那么本次笔记就对视图的世界进行深入的剖析. 视图是通 ...
- SQL Server数据库视图
1:什么是视图 2:视图和查询的区别 3:视图的优点 4:如何创建和管理视图 5:如何通过视图修改基本表的数据 6:如何通过视图实现数据的安全性 A:什么是视图: 视图(view):从一个或几个基本表 ...
随机推荐
- js获取iframe里的body内容
做个页面 需要加入a.html 使用的js动态添加iframe 直接JQ添加的 代码 $(".banner-box").after(“<iframe src="ht ...
- Security Test Cases
https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents Username Enumeration Vulner ...
- Java Runtime.availableProcessors()方法
Java Runtime.availableProcessors()方法用法实例教程. 描述 java.lang.Runtime.availableProcessors() 方法返回到Java虚拟 ...
- [js/jquery]移动端手势拖动,放大,缩小预览图片
摘要 有这样的需求需要在手机端预览图片的时候,实现图片的手势拖动,放大缩小功能.最终通过touch.js这个插件实现了效果. touch.js Touch.js是移动设备上的手势识别与事件库, 由百度 ...
- Spring Data JPA 的配置文件 已经数据库的状态
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- linux命令--nslookup
nslookup和dig都是非常有用的网络命令,简单而言,就是查dns信息用的. 本地的dns配置文件在哪里? 文件是:/etc/resolv.conf 打开这个文件:vi /etc/resolv.c ...
- oracle 中的trunc()函数及加一个月,一天,一小时,一分钟,一秒钟方法
返回处理后的数据,不同于round()(对数值进行四舍五入处理),该函数不对指定小数前或后的数值部分进行舍入处理. 语法:trunc(number[,decimals]) 其中,number为待做处理 ...
- Unity相关路径
Application.dataPath 只读 在项目根目录下读取文件,但移动端没有访问权限.一般适用于PC端调试用. Application.streamingAssetsPath 在Assets目 ...
- 自定义select控件开发
目的:select下拉框条目太多(上百),当用户选择具体项时会浪费用户很多时间去寻找,因此需要一个搜索框让用户输入关键字来匹配列表,便于用户选择 示例图: 1.html结构 <div class ...
- SQL 获取查询IO信息
DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO ON --开启IO统计 SET STATISTICS TIME ON -- 开启耗时统计 <code ...