ASP.NET 之 EntityFramework实体框架搭建
前段时间接触了EntityFramework,对ORM框架也是有了初步的认识,现在对其进行一点小总结。
一、ORM简介

对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中。是不是还是不懂?那我们把ORM拆开来说:
O 创建简单的实体对象,也就是数据Model
R 关系数据库中数据表
M 把实体对象与关系数据库具体数据表关联起来,产生相应的SQL操作
在对象中主要利用 特性 来标识主外键、字段长度、默认值等。
二、EntityFramework的安装
- 在解决方案处单击右键
- 管理解决方案的NuGet程序包
- 在搜索框中搜索EntityFramework
- 勾选要安装的项目
- 安装EF

三、EntityFramework的简单配置
- 对Web.config中的连接数据库的字符串进行配置
<connectionStrings>
<add name="DefaultConnection" connectionString="server=.;database=OnLineExamDB;uid=sa;pwd=123456;" providerName="System.Data.SqlClient" />
</connectionStrings>server=.;代表为本地、OnLineExamDB为我连接的数据库的名称、uid跟pwd是连接数据库的用户密码
- 配置Model层、主键要用[Key]进行标识并引用命名空间System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Model
{
public class Class
{
[Key]
//主键要加上[Key],并引用命名空间System.ComponentModel.DataAnnotations;
public int ClassID { get; set; } /// <summary>
/// 班级名称
/// </summary>
public string ClassName { get; set; } }
}在DAL数据层创建DBFactory并继承DbContext,配置Model实体与表的关系
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Model; namespace DAL
{
public class DBFactory : DbContext
{
public DBFactory() : base("DefaultConnection")
{ } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//配置Model实体与表的关系
modelBuilder.Entity<Class>().ToTable("Class");
modelBuilder.Entity<Student>().ToTable("Student");
base.OnModelCreating(modelBuilder);
} // 班级数据工厂
public DbSet<Class> ClassFactory { get; set; } // 学生数据工厂
public DbSet<Student> StudentFactory { get; set; }
}
}
- 在BLL业务层中创建StudentService,查询数据库数据
FirstOrDefault为返回序列中的第一个元素,如果没有该元素就返回默认值。
namespace BLL
{
public class StudentService
{
DBFactory dbFactory = new DBFactory(); public Student GetStudent()
{
return dbFactory.StudentFactory.FirstOrDefault();
}
}
}在Controller中配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using OnLineExam.Models;
using System.Web.Security; namespace OnLineExam.Controllers
{
public class UserInfoController : Controller
{
StudentService StudentService = new StudentService(); public ActionResult Index()
{
//获取学生Model
var student = StudentService.GetStudent();
//存入ViewData用于获取
ViewData["Student"] = student;
return View();
}
}
}- 添加视图
@using Model;
@{
var student = ViewData["Student"] as Student;
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
学生名称:@student.StudentName
</div>
<div>
学生编号:@student.StudentNumber
</div>
</body>
</html>
结果显示:

这样一个使用EntityFramework的ORM框架就成功实现了。
ASP.NET 之 EntityFramework实体框架搭建的更多相关文章
- 用实体框架搭建MVC程序框架(全部)
第一步:1.新建项目 2.新建domain类库 3.新建Data类库 4.为上面的1.2.3添加实体框架nuget包.(可以右键管理nuget包来查找entityframework,当然也可以通过程序 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- .NET实体框架EF之CodeFirst
ADO.NET Entity Framework 以 Entity Data Model (EDM) 为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Sch ...
- [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现
上篇已经设计出比较完善的数据库了,这篇开始进入代码. 首先把上篇设计的数据库脚本在数据库中执行下,生成数据库,然后在VS中建立项目,为了方便理解和查看,我设计的都是很直白的类名和文件名,没有命名空间 ...
- asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
学习asp.net 已经有近三个月的时间了,在asp.net mvc上花的时间最多,但个人真是有些菜,不得不说,asp.net mvc的水真的还是蛮深的.目前在公司实习,也见过公司几个项目的代码了.对 ...
- 从实体框架核心开始:构建一个ASP。NET Core应用程序与Web API和代码优先开发
下载StudentApplication.Web.zip - 599.5 KB 下载StudentApplication.API.zip - 11.5 KB 介绍 在上一篇文章中,我们了解了实体框架的 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (21) -----第四章 ASP.NET MVC中使用实体框架之在页面中创建查询和使用ASP.NET URL路由过虑
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 4.2. 构建一个搜索查询 搜索数据是几乎所有应用的一个基本功能.它一般是动态的,因 ...
- 实体框架高级应用之动态过滤 EntityFramework DynamicFilters
实体框架高级应用之动态过滤 EntityFramework DynamicFilters 我们开门见山,直奔主题. 一.EntityFramework DynamicFilters 是什么,它能做什么 ...
- MVC之实体框架(数据持久化框架)EntityFrameWork(EF)
EF - EntityFrameWork 中文名:实体框架(数据持久化框架) 1.使用EF查询(Linq to EF) 1.1使用标准查询运算符来查询 OumindBlogEntities db = ...
随机推荐
- 调用GOOGLE的TTS实现文字转语音(XE7+小米2)(XE10.1+小米5)
相关资料: 注意:在手机上必须选安装文字转语音引擎“google Text To Speech”地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYi ...
- [leetcode 14]Longest Common Prfix
1 题目: Write a function to find the longest common prefix string amongst an array of strings. Hide Ta ...
- 一些LinuxC的小知识点(一)
以下代码在Federo9上试验成功. 一.格式化输入16进制字符串 printf(); 输入结果: 二.测试各类型的占用的字节数 int main(int argc, char *argv[]) { ...
- NPOI 操作笔记
public static class ExcelUtil { // Methods public static DataTable GetDataTable(Stream stream) { HSS ...
- springBoot2 基础语法
请求响应 request对象 request 对象其实是HttpServletRequest 类型, 通过它可以获取请求相关的一切信息, 包含请求信息 . 以及请求参数 ,甚至可以当成往里面存储数据[ ...
- Django 实现第三方账号登录网站
这里我们使用 django-allauth 模块来实现第三方账号验证登录,官方文档如下:https://django-allauth.readthedocs.io/en/latest/ . 安装 dj ...
- lost+found目录有啥用?
Linux系统中根目录下或者新挂载的磁盘目录下有一个叫lost+found,它的作用是什么? 如果你运行fsck命令(文件系统检查和修复命令),它也许会找到一些数据碎片,这些文件碎片在硬盘中并没有引用 ...
- centos7上mysql5.6版本主从复制
做主从复制实验: 第一步:主服务器上操作 1.修改主服务器master: [root@localhost ~]# vim /etc/my.cnf server_id = 1 //[必须]服务器唯一I ...
- postgresql-int,bigint,numeric效率测试
在postgresql9.5的时候做过一个测试就是sum()的效率最终的测试结果是sum(int)>sum(numeric)>sum(bigint)当时比较诧异为啥sum(bigint)效 ...
- JS: 数组的循环函数
JS 数组相关的循环函数,用得挺多,所以有些坑还是要去踩一下,先来看一道面试题. 注意:下面提到的不改变原数组仅针对基本数据类型. 面试题 模拟实现数组的 map 函数. 心中有答案了吗?我的答案放在 ...