Entity Framework之DB First方式詳解
EF(Entity Framework的簡稱,下同)有三種方式,分別是:DataBase First、 Model First和Code First。
下面是Db First的方式:
1. 數(shù)據(jù)庫庫中存在兩個(gè)表,一個(gè)是專業(yè)表,一個(gè)學(xué)生表,一個(gè)學(xué)生只能屬于一個(gè)專業(yè):

其中T_Major是專業(yè)表,T_Student是學(xué)生表,StudentId是學(xué)號(hào),MajorId是專業(yè)Id,T_Major與T_Student是一對(duì)多的關(guān)系。
2. 項(xiàng)目中添加數(shù)據(jù)庫實(shí)體模型



因?yàn)橹皼]有配置過數(shù)據(jù)庫連接,所以點(diǎn)擊“新建庫連接”,如果之前配置過數(shù)據(jù)庫連接,可以直接從下拉列表中選擇或者新建



選擇需要生成的表/存儲(chǔ)過程等

點(diǎn)擊“完成”

這里會(huì)彈出如下圖的窗口,然后選擇確定(如果再彈出,也選擇確定),如果不小心點(diǎn)擊了取消,可以在模型設(shè)計(jì)界面Ctrl + S(保存的快捷鍵),或如下圖的操作,然后會(huì)彈出窗口,一直確定就行。


這里是使用MVC,所以添加一個(gè)控制器來測試(這里為了快速生成讀寫的控制器方法,選擇“包含讀/寫操作的MVC5控制器”)


生成代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Zhong.Web.Controllers
{
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}
// GET: Student/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Student/Create
public ActionResult Create()
{
return View();
}
// POST: Student/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Student/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Student/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Student/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Student/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
同樣的方法添加一個(gè)Major控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Zhong.Web.Controllers
{
public class MajorController : Controller
{
// GET: Major
public ActionResult Index()
{
return View();
}
// GET: Major/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Major/Create
public ActionResult Create()
{
return View();
}
// POST: Major/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Major/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Major/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Major/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Major/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
由于學(xué)生表MajorId依賴于Major表,所以需要先有專業(yè),才能新增學(xué)生數(shù)據(jù)(這里不討論是否合理)
編寫邏輯代碼,創(chuàng)建視圖
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zhong.Web.Models;
namespace Zhong.Web.Controllers
{
public class MajorController : Controller
{
// GET: Major
public ActionResult Index()
{
var majors = new EFDbEntities().T_Major.ToList();
return View(majors);
}
// GET: Major/Details/5
public ActionResult Details(int id)
{
var major = new EFDbEntities().T_Major.Find(id);
if (major == null)
{
return Content("參數(shù)錯(cuò)誤");
}
return View(major);
}
// GET: Major/Create
public ActionResult Create()
{
return View();
}
// POST: Major/Create
[HttpPost]
public ActionResult Create(T_Major entity)
{
if (entity != null)
{
var entities = new EFDbEntities();
entities.T_Major.Add(entity);
entities.SaveChanges();
}
return RedirectToAction("Index");
}
// GET: Major/Edit/5
public ActionResult Edit(int id)
{
var entity = new EFDbEntities().T_Major.Find(id);
if (entity == null)
{
return Content("參數(shù)錯(cuò)誤");
}
return View(entity);
}
// POST: Major/Edit/5
[HttpPost]
public ActionResult Edit(T_Major entity)
{
if (entity == null)
{
return Content("參數(shù)錯(cuò)誤");
}
var entities = new EFDbEntities();
#region 方式一
////該方式一般是根據(jù)主鍵先讀取數(shù)據(jù),然后再逐個(gè)賦值,最后更新
//var oldEntity = entities.T_Major.Find(entity.Id);
//if (oldEntity!=null)
//{
// oldEntity.Name = entity.Name;
// entities.SaveChanges();
//}
#endregion
#region 方式二
//該方式是直接將新的實(shí)體(可能是new出來的并且對(duì)主鍵等的屬性賦值好了)附加到上下文,然后標(biāo)記狀態(tài)為修改Modified
entities.T_Major.Attach(entity);
entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
entities.SaveChanges();
#endregion
return RedirectToAction("Index");
}
// GET: Major/Delete/5
public ActionResult Delete(int id)
{
var major = new EFDbEntities().T_Major.Find(id);
return View(major);
}
// POST: Major/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
var entities = new EFDbEntities();
var major = entities.T_Major.Find(id);
entities.T_Major.Remove(major);
entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
添加專業(yè):

專業(yè)列表:

同樣實(shí)現(xiàn)學(xué)生控制器與視圖:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zhong.Web.Models;
namespace Zhong.Web.Controllers
{
public class StudentController : Controller
{
private EFDbEntities entities = new EFDbEntities();
// GET: Student
public ActionResult Index()
{
var students = entities.T_Student.ToList();
return View(students);
}
// GET: Student/Details/5
public ActionResult Details(int id)
{
var student = entities.T_Student.Find(id);
return View(student);
}
// GET: Student/Create
public ActionResult Create()
{
ViewData["MajorId"] = entities.T_Major.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
return View();
}
// POST: Student/Create
[HttpPost]
public ActionResult Create(T_Student entity)
{
entities.T_Student.Add(entity);
entities.SaveChanges();
return RedirectToAction("Index");
}
// GET: Student/Edit/5
public ActionResult Edit(int id)
{
var student = entities.T_Student.Find(id);
ViewData["MajorId"] = entities.T_Major.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
return View(student);
}
// POST: Student/Edit/5
[HttpPost]
public ActionResult Edit(T_Student entity)
{
if (entity == null)
{
return Content("參數(shù)錯(cuò)誤");
}
entities.T_Student.Attach(entity);
entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
entities.SaveChanges();
return RedirectToAction("Index");
}
// GET: Student/Delete/5
public ActionResult Delete(int id)
{
var student = entities.T_Student.Find(id);
return View(student);
}
// POST: Student/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
var student = entities.T_Student.Find(id);
entities.T_Student.Remove(student);
entities.SaveChanges();
return RedirectToAction("Index");
}
}
}
添加學(xué)生時(shí),報(bào)錯(cuò)如下:

于是在控制器中增加如下代碼:

刷新頁面:

編輯:

刪除:

列表:

在MajorController中有介紹EF的兩種更新方式。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET?MVC通過勾選checkbox更改select的內(nèi)容
這篇文章介紹了ASP.NET?MVC通過勾選checkbox更改select內(nèi)容的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Json返回時(shí)間的格式中出現(xiàn)亂碼問題的兩種解決方案
使用Json返回?cái)?shù)據(jù)的時(shí)候時(shí)間的格式一般都會(huì)變了,變成我們不認(rèn)識(shí)的一些字符,那么當(dāng)我們遇到這些問題的時(shí)候我們?cè)撛趺唇鉀Q呢,今天我就來小說一下這個(gè)的解決方法2013-10-10
Extjs4.1.x 框架搭建 采用Application動(dòng)態(tài)按需加載MVC各模塊完美實(shí)現(xiàn)
中午的時(shí)候發(fā)了第一篇 Extjs4.1.x 框架搭建 采用Application動(dòng)態(tài)按需加載MVC各模塊,發(fā)現(xiàn)實(shí)現(xiàn)上還是有問題,本文將提供詳細(xì)的完美方案2012-11-11
.Net微信網(wǎng)頁開發(fā)解決用戶在不同公眾號(hào)或在公眾號(hào)、移動(dòng)應(yīng)用之間帳號(hào)統(tǒng)一問題
這篇文章主要介紹了.Net微信網(wǎng)頁開發(fā)解決用戶在不同公眾號(hào)或在公眾號(hào)、移動(dòng)應(yīng)用之間帳號(hào)統(tǒng)一問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
.net開發(fā)中批量刪除記錄時(shí)實(shí)現(xiàn)全選功能的具體方法
這篇文章介紹了.net開發(fā)中批量刪除記錄時(shí)實(shí)現(xiàn)全選功能的具體方法,有需要的朋友可以參考一下2013-11-11
ASP.NET Core3.1 Ocelot路由的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core3.1 Ocelot路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
asp.net實(shí)現(xiàn)在非MVC中使用Razor模板引擎的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)在非MVC中使用Razor模板引擎的方法,較為詳細(xì)的分析了Razor模板引擎的使用技巧,需要的朋友可以參考下2015-06-06
使用 .NET MAUI 開發(fā) ChatGPT 客戶端的流程
最近?chatgpt?很火,由于網(wǎng)頁版本限制了 ip,還得必須開代理,用起來比較麻煩,所以我嘗試用 maui 開發(fā)一個(gè)聊天小應(yīng)用,結(jié)合 chatgpt 的開放 api 來實(shí)現(xiàn),這篇文章主要介紹了使用 .NET MAUI 開發(fā) ChatGPT 客戶端,需要的朋友可以參考下2022-12-12

