Entity Framework-DB First method, entityframework
EF (hereinafter referred to as Entity Framework) has three methods: DataBase First, Model First, and Code First.
The following is the Db First method:
1. There are two tables in the database. One is a professional table and the other is a student table. One student can only belong to one major:
Specifically, T_Major is a professional table, T_Student is a student table, StudentId is a student Id, MajorId is a professional Id, and T_Major is a one-to-many relationship with T_Student.
2. Add a database entity model to the Project
Because no database connection has been configured before, click "Create Database Connection". If you have configured a database connection before, select or create a database from the drop-down list.
Select the table/stored procedure to be generated
Click "finish"
A window such as is displayed, and then click OK. (If yes, click OK, you can press Ctrl + S (saved shortcut key) on the model design interface, or perform the following operations. Then, a window is displayed, and you can click OK.
Here MVC is used, so add a controller for testing (here to quickly generate a read/write Controller method, select "MVC5 controller containing read/write operations ")
The generated code is as follows:
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(); } } }}
View Code
Add a Major controller in the same way
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(); } } }}
View Code
Because the student table MajorId depends on the Major table, you need to have a Major first to add student data (it is not discussed here if it is reasonable)
Write logic code to create a view
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 EFDb Entities (). t_Major.Find (id); if (major = null) {return Content ("parameter error");} 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 ("parameter error");} return View (entity);} // POST: major/Edit/5 [HttpPost] public ActionResult Edit (T_Major entity) {if (entity = Null) {return Content ("parameter error");} var entities = new EFDbEntities (); # region method 1 // This method is generally used to read data based on the primary key, assign values one by one, and finally update // var oldEntity = entities. t_Major.Find (entity. id); // if (oldEntity! = Null) // {// oldEntity. name = entity. name; // entities. saveChanges (); //} # endregion # region method 2 // This method directly attaches a new object (it may be new and has been assigned a value to attributes such as the primary key) attach to context, and then mark the status as 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 ();}}}}
View Code
Add major:
Professional list:
Similarly, student controllers and views are implemented:
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 ("parameter error");} 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 ");}}}
View Code
When a student is added, the following error is reported:
Add the following code to the Controller:
Refresh page:
Edit:
Delete:
List:
Two update Methods for EF are introduced in MajorController.