一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

2020-05-13 15:28zhjchhahaha ASP.NET教程

這篇文章主要為大家詳細介紹了Entity Framework三種方式之一DataBase First,具有一定的參考價值,感興趣的小伙伴們可以參考一下

EF(Entity Framework的簡稱,下同)有三種方式,分別是:DataBase First、 Model First和Code First。

下面是Db First的方式:

1. 數據庫庫中存在兩個表,一個是專業表,一個學生表,一個學生只能屬于一個專業:

Entity Framework之DB First方式詳解

其中T_Major是專業表,T_Student是學生表,StudentId是學號,MajorId是專業Id,T_Major與T_Student是一對多的關系。

2. 項目中添加數據庫實體模型

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

因為之前沒有配置過數據庫連接,所以點擊“新建庫連接”,如果之前配置過數據庫連接,可以直接從下拉列表中選擇或者新建

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

選擇需要生成的表/存儲過程等

Entity Framework之DB First方式詳解

點擊“完成”

Entity Framework之DB First方式詳解

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

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

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

Entity Framework之DB First方式詳解

Entity Framework之DB First方式詳解

生成代碼如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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();
      }
    }
  }
}

同樣的方法添加一個Major控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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();
      }
    }
  }
}

由于學生表MajorId依賴于Major表,所以需要先有專業,才能新增學生數據(這里不討論是否合理)

編寫邏輯代碼,創建視圖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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("參數錯誤");
      }
      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("參數錯誤");
      }
      return View(entity);
    }
 
    // POST: Major/Edit/5
    [HttpPost]
    public ActionResult Edit(T_Major entity)
    {
      if (entity == null)
      {
        return Content("參數錯誤");
      }
      var entities = new EFDbEntities();
      #region 方式一
      ////該方式一般是根據主鍵先讀取數據,然后再逐個賦值,最后更新
      //var oldEntity = entities.T_Major.Find(entity.Id);
      //if (oldEntity!=null)
      //{
      //  oldEntity.Name = entity.Name;
      //  entities.SaveChanges();
      //}
      #endregion
 
      #region 方式二
      //該方式是直接將新的實體(可能是new出來的并且對主鍵等的屬性賦值好了)附加到上下文,然后標記狀態為修改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();
      }
    }
  }
}

添加專業:

Entity Framework之DB First方式詳解

專業列表:

Entity Framework之DB First方式詳解

同樣實現學生控制器與視圖:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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("參數錯誤");
      }
      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");
    }
  }
}

添加學生時,報錯如下:

Entity Framework之DB First方式詳解

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

Entity Framework之DB First方式詳解

刷新頁面:

Entity Framework之DB First方式詳解

編輯:

Entity Framework之DB First方式詳解

刪除:

Entity Framework之DB First方式詳解

列表:

Entity Framework之DB First方式詳解

在MajorController中有介紹EF的兩種更新方式。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: tube日本高清老少配 | 日本卡一卡2卡3卡4精品卡无人区 | 欧美日韩专区国产精品 | h日本漫画全彩在线观看 | 免费观看成年肉动漫网站 | 日本久久免费大片 | 天天做天天爱天天一爽一毛片 | 久久精品一区二区免费看 | 国产日产精品久久久久快鸭 | 视频一区二区 村上凉子 | 99热这里只有精品在线 | 久久久久久久久性潮 | 久久这里只精品国产99re66 | 国产va免费精品高清在线观看 | 精品综合久久久久久8888 | 国产资源在线视频 | 36美女厕所撒尿全过程 | 亚洲精品影视 | 国内精品91东航翘臀女神在线 | 毛茸茸的大逼 | 逼毛片 | ova巨公主催眠1在线观看 | 动漫a级片 | 亚洲精品αv一区二区三区 亚洲精品91大神在线观看 | 免费欧美一级 | 强行扒开美女大腿挺进 | 福利社在线免费观看 | 五月婷婷伊人网 | 亚洲高清国产拍精品影院 | 人妖欧美一区二区三区四区 | 五月天色综合 | 香蕉国产人午夜视频在线观看 | 天海翼黄色三级 | 欧美日韩亚洲一区二区三区在线观看 | beeg最新 | 成人网18免费网 | 草草视频免费观看 | 亚洲AV永久无码精品澳门 | 娇妻与老头绿文小说系列 | 成人网子 | 无罩看奶禁18 |