1. 程式人生 > >ASP.NET 之 EntityFramework實體框架搭建

ASP.NET 之 EntityFramework實體框架搭建

一個 eric null ron config .com span gif div

  前段時間接觸了EntityFramework,對ORM框架也是有了初步的認識,現在對其進行一點小總結。

一、ORM簡介

技術分享圖片

  對象關系映射(Object Relational Mapping,簡稱ORM)模式是一種為了解決面向對象與關系數據庫存在的互不匹配的現象的技術。簡單的說,ORM是通過使用描述對象和數據庫之間映射的元數據,將程序中的對象自動持久化到關系數據庫中。是不是還是不懂?那我們把ORM拆開來說:

O 創建簡單的實體對象,也就是數據Model

R 關系數據庫中數據表

M 把實體對象與關系數據庫具體數據表關聯起來,產生相應的SQL操作

  在對象中主要利用 特性 來標識主外鍵、字段長度、默認值等。

二、EntityFramework的安裝

  1. 在解決方案處單擊右鍵
  2. 管理解決方案的NuGet程序包
  3. 在搜索框中搜索EntityFramework
  4. 勾選要安裝的項目
  5. 安裝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實體與表的關系

    技術分享圖片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();
            }
        }
    }
    技術分享圖片
  • 添加視圖 技術分享圖片 技術分享圖片
    @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實體框架搭建