前段時間接觸了EntityFramework,對ORM框架也是有了初步的認識,現在對其進行一點小總結。 一、ORM簡介 對象關係映射(Object Relational Mapping,簡稱ORM)模式是一種為瞭解決面向對象與關係資料庫存在的互不匹配的現象的技術。簡單的說,ORM是通過使用描述對象和 ...
前段時間接觸了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; } } }
View Code在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; } } }
View Code
- 在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(); } } }
View Code - 添加視圖
@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>
View Code
結果顯示:
這樣一個使用EntityFramework的ORM框架就成功實現了。