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 = ...
随机推荐
- 1. Ubuntu下MongoDB的安装和使用
一.MongoDB介绍 MongoDB 是一个是一个基于分布式文件存储的数据库,介于关系数据库和非关系数据库之间,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似jso ...
- 使用PerfView监测.NET程序性能(一):Event Trace for Windows
前言: 在日常项目开发中,我们时不时会遇到程序占用了很高CPU的情况,可能是程序里某些未经优化的代码或者Bug,或者是程序运行压力太大.无论是什么原因,我们总希望能看到到底是哪个方法占用了如此高的CP ...
- node-webkit学习(1)hello world
)hello world 文/玄魂 目录 node-webkit学习(1)hello world 前言 1.1 环境安装 1.1.1 windows下的安装 1.1.2 linux环境下的安装 1 ...
- nuget.org无法解析的办法
今天想学习ef框架,就着手安装最新的ef啦.可是遇到了问题,提示 未能解析此远程名称:'nuget.org' 就去上网找资料啦,发现原来是被墙了,表示无奈. 网上的资料提示,修改hosts文件或是dn ...
- WPF实战案例-打印
在前段时间做了一下打印,因为需要支持的格式比较多,所以wpf能打印的有限分享一下几种格式的打印(.xls .xlsx .doc .docx .png .jpg .bmp .pdf) 首先为了保证exc ...
- 背水一战 Windows 10 (43) - C# 7.0 新特性
[源码下载] 背水一战 Windows 10 (43) - C# 7.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 7.0 新特性 介绍 C# 7.0 的新特性 示例 ...
- Effective C++笔记:继承与面向对象设计
关于OOP 博客地址:http://www.cnblogs.com/ronny 转载请注明出处! 1,继承可以是单一继承或多重继承,每一个继承连接可以是public.protected或private ...
- [JSOI2018]列队(主席树)
跟上次那道列队不一样,但都是九条可怜...(吉老师太强了) 在主席树上统计答案,因为值域只有 \(10^6\) 甚至不用离散化... \(Code\ Below:\) #include <bit ...
- vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件
vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件/库 一提到移动端的下拉刷新上拉翻页,你可能就会想到iScroll插件,没错iScroll是一个高性能,资源 ...
- redis安装(linux)
redis安装 1. 安装tcl # cd /usr/local # wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz # t ...