查看: 2169|回复: 0

[.NET开发] Entity Framework之DB First方式详解

发表于 2018-1-2 08:00:00

EF(Entity Framework的简称,下同)有三种方式,分别是:DataBase First、 Model First和Code First。

下面是Db First的方式:

1. 数据库库中存在两个表,一个是专业表,一个学生表,一个学生只能属于一个专业:

其中T_Major是专业表,T_Student是学生表,StudentId是学号,MajorId是专业Id,T_Major与T_Student是一对多的关系。

2. 项目中添加数据库实体模型

因为之前没有配置过数据库连接,所以点击“新建库连接”,如果之前配置过数据库连接,可以直接从下拉列表中选择或者新建

选择需要生成的表/存储过程等

点击“完成”

这里会弹出如下图的窗口,然后选择确定(如果再弹出,也选择确定),如果不小心点击了取消,可以在模型设计界面Ctrl + S(保存的快捷键),或如下图的操作,然后会弹出窗口,一直确定就行。

这里是使用MVC,所以添加一个控制器来测试(这里为了快速生成读写的控制器方法,选择“包含读/写操作的MVC5控制器”)

生成代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace Zhong.Web.Controllers
  7. {
  8. public class StudentController : Controller
  9. {
  10. // GET: Student
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15. // GET: Student/Details/5
  16. public ActionResult Details(int id)
  17. {
  18. return View();
  19. }
  20. // GET: Student/Create
  21. public ActionResult Create()
  22. {
  23. return View();
  24. }
  25. // POST: Student/Create
  26. [HttpPost]
  27. public ActionResult Create(FormCollection collection)
  28. {
  29. try
  30. {
  31. // TODO: Add insert logic here
  32. return RedirectToAction("Index");
  33. }
  34. catch
  35. {
  36. return View();
  37. }
  38. }
  39. // GET: Student/Edit/5
  40. public ActionResult Edit(int id)
  41. {
  42. return View();
  43. }
  44. // POST: Student/Edit/5
  45. [HttpPost]
  46. public ActionResult Edit(int id, FormCollection collection)
  47. {
  48. try
  49. {
  50. // TODO: Add update logic here
  51. return RedirectToAction("Index");
  52. }
  53. catch
  54. {
  55. return View();
  56. }
  57. }
  58. // GET: Student/Delete/5
  59. public ActionResult Delete(int id)
  60. {
  61. return View();
  62. }
  63. // POST: Student/Delete/5
  64. [HttpPost]
  65. public ActionResult Delete(int id, FormCollection collection)
  66. {
  67. try
  68. {
  69. // TODO: Add delete logic here
  70. return RedirectToAction("Index");
  71. }
  72. catch
  73. {
  74. return View();
  75. }
  76. }
  77. }
  78. }
复制代码

同样的方法添加一个Major控制器

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace Zhong.Web.Controllers
  7. {
  8. public class MajorController : Controller
  9. {
  10. // GET: Major
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15. // GET: Major/Details/5
  16. public ActionResult Details(int id)
  17. {
  18. return View();
  19. }
  20. // GET: Major/Create
  21. public ActionResult Create()
  22. {
  23. return View();
  24. }
  25. // POST: Major/Create
  26. [HttpPost]
  27. public ActionResult Create(FormCollection collection)
  28. {
  29. try
  30. {
  31. // TODO: Add insert logic here
  32. return RedirectToAction("Index");
  33. }
  34. catch
  35. {
  36. return View();
  37. }
  38. }
  39. // GET: Major/Edit/5
  40. public ActionResult Edit(int id)
  41. {
  42. return View();
  43. }
  44. // POST: Major/Edit/5
  45. [HttpPost]
  46. public ActionResult Edit(int id, FormCollection collection)
  47. {
  48. try
  49. {
  50. // TODO: Add update logic here
  51. return RedirectToAction("Index");
  52. }
  53. catch
  54. {
  55. return View();
  56. }
  57. }
  58. // GET: Major/Delete/5
  59. public ActionResult Delete(int id)
  60. {
  61. return View();
  62. }
  63. // POST: Major/Delete/5
  64. [HttpPost]
  65. public ActionResult Delete(int id, FormCollection collection)
  66. {
  67. try
  68. {
  69. // TODO: Add delete logic here
  70. return RedirectToAction("Index");
  71. }
  72. catch
  73. {
  74. return View();
  75. }
  76. }
  77. }
  78. }
复制代码

由于学生表MajorId依赖于Major表,所以需要先有专业,才能新增学生数据(这里不讨论是否合理)

编写逻辑代码,创建视图

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Zhong.Web.Models;
  7. namespace Zhong.Web.Controllers
  8. {
  9. public class MajorController : Controller
  10. {
  11. // GET: Major
  12. public ActionResult Index()
  13. {
  14. var majors = new EFDbEntities().T_Major.ToList();
  15. return View(majors);
  16. }
  17. // GET: Major/Details/5
  18. public ActionResult Details(int id)
  19. {
  20. var major = new EFDbEntities().T_Major.Find(id);
  21. if (major == null)
  22. {
  23. return Content("参数错误");
  24. }
  25. return View(major);
  26. }
  27. // GET: Major/Create
  28. public ActionResult Create()
  29. {
  30. return View();
  31. }
  32. // POST: Major/Create
  33. [HttpPost]
  34. public ActionResult Create(T_Major entity)
  35. {
  36. if (entity != null)
  37. {
  38. var entities = new EFDbEntities();
  39. entities.T_Major.Add(entity);
  40. entities.SaveChanges();
  41. }
  42. return RedirectToAction("Index");
  43. }
  44. // GET: Major/Edit/5
  45. public ActionResult Edit(int id)
  46. {
  47. var entity = new EFDbEntities().T_Major.Find(id);
  48. if (entity == null)
  49. {
  50. return Content("参数错误");
  51. }
  52. return View(entity);
  53. }
  54. // POST: Major/Edit/5
  55. [HttpPost]
  56. public ActionResult Edit(T_Major entity)
  57. {
  58. if (entity == null)
  59. {
  60. return Content("参数错误");
  61. }
  62. var entities = new EFDbEntities();
  63. #region 方式一
  64. ////该方式一般是根据主键先读取数据,然后再逐个赋值,最后更新
  65. //var oldEntity = entities.T_Major.Find(entity.Id);
  66. //if (oldEntity!=null)
  67. //{
  68. // oldEntity.Name = entity.Name;
  69. // entities.SaveChanges();
  70. //}
  71. #endregion
  72. #region 方式二
  73. //该方式是直接将新的实体(可能是new出来的并且对主键等的属性赋值好了)附加到上下文,然后标记状态为修改Modified
  74. entities.T_Major.Attach(entity);
  75. entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
  76. entities.SaveChanges();
  77. #endregion
  78. return RedirectToAction("Index");
  79. }
  80. // GET: Major/Delete/5
  81. public ActionResult Delete(int id)
  82. {
  83. var major = new EFDbEntities().T_Major.Find(id);
  84. return View(major);
  85. }
  86. // POST: Major/Delete/5
  87. [HttpPost]
  88. public ActionResult Delete(int id, FormCollection collection)
  89. {
  90. try
  91. {
  92. // TODO: Add delete logic here
  93. var entities = new EFDbEntities();
  94. var major = entities.T_Major.Find(id);
  95. entities.T_Major.Remove(major);
  96. entities.SaveChanges();
  97. return RedirectToAction("Index");
  98. }
  99. catch
  100. {
  101. return View();
  102. }
  103. }
  104. }
  105. }
复制代码

添加专业:

专业列表:

同样实现学生控制器与视图:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Zhong.Web.Models;
  7. namespace Zhong.Web.Controllers
  8. {
  9. public class StudentController : Controller
  10. {
  11. private EFDbEntities entities = new EFDbEntities();
  12. // GET: Student
  13. public ActionResult Index()
  14. {
  15. var students = entities.T_Student.ToList();
  16. return View(students);
  17. }
  18. // GET: Student/Details/5
  19. public ActionResult Details(int id)
  20. {
  21. var student = entities.T_Student.Find(id);
  22. return View(student);
  23. }
  24. // GET: Student/Create
  25. public ActionResult Create()
  26. {
  27. ViewData["MajorId"] = entities.T_Major.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
  28. return View();
  29. }
  30. // POST: Student/Create
  31. [HttpPost]
  32. public ActionResult Create(T_Student entity)
  33. {
  34. entities.T_Student.Add(entity);
  35. entities.SaveChanges();
  36. return RedirectToAction("Index");
  37. }
  38. // GET: Student/Edit/5
  39. public ActionResult Edit(int id)
  40. {
  41. var student = entities.T_Student.Find(id);
  42. ViewData["MajorId"] = entities.T_Major.Select(m => new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
  43. return View(student);
  44. }
  45. // POST: Student/Edit/5
  46. [HttpPost]
  47. public ActionResult Edit(T_Student entity)
  48. {
  49. if (entity == null)
  50. {
  51. return Content("参数错误");
  52. }
  53. entities.T_Student.Attach(entity);
  54. entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
  55. entities.SaveChanges();
  56. return RedirectToAction("Index");
  57. }
  58. // GET: Student/Delete/5
  59. public ActionResult Delete(int id)
  60. {
  61. var student = entities.T_Student.Find(id);
  62. return View(student);
  63. }
  64. // POST: Student/Delete/5
  65. [HttpPost]
  66. public ActionResult Delete(int id, FormCollection collection)
  67. {
  68. var student = entities.T_Student.Find(id);
  69. entities.T_Student.Remove(student);
  70. entities.SaveChanges();
  71. return RedirectToAction("Index");
  72. }
  73. }
  74. }
复制代码

添加学生时,报错如下:

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

刷新页面:

编辑:

删除:

列表:

在MajorController中有介绍EF的两种更新方式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报