欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

EntityFramework之DBFirst方式有什么用-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)Entity Framework之DB First方式有什么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計,網(wǎng)站模板,微信公眾號開發(fā),軟件開發(fā),微信小程序,十載建站對發(fā)電機租賃等多個方面,擁有豐富的網(wǎng)站設(shè)計經(jīng)驗。

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

下面是Db First的方式:

1. 數(shù)據(jù)庫庫中存在兩個表,一個是專業(yè)表,一個學(xué)生表,一個學(xué)生只能屬于一個專業(yè):

Entity Framework之DB First方式有什么用

其中T_Major是專業(yè)表,T_Student是學(xué)生表,StudentId是學(xué)號,MajorId是專業(yè)Id,T_Major與T_Student是一對多的關(guān)系。

2. 項目中添加數(shù)據(jù)庫實體模型

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

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

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

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

Entity Framework之DB First方式有什么用

點擊“完成”

Entity Framework之DB First方式有什么用

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

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

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

Entity Framework之DB First方式有什么用

Entity Framework之DB First方式有什么用

生成代碼如下:

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控制器

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ù)錯誤");
      }
      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ù)錯誤");
      }
      return View(entity);
    }

    // POST: Major/Edit/5
    [HttpPost]
    public ActionResult Edit(T_Major entity)
    {
      if (entity == null)
      {
        return Content("參數(shù)錯誤");
      }
      var entities = new EFDbEntities();
      #region 方式一 
      ////該方式一般是根據(jù)主鍵先讀取數(shù)據(jù),然后再逐個賦值,最后更新
      //var oldEntity = entities.T_Major.Find(entity.Id);
      //if (oldEntity!=null)
      //{
      //  oldEntity.Name = entity.Name;
      //  entities.SaveChanges();
      //}
      #endregion

      #region 方式二
      //該方式是直接將新的實體(可能是new出來的并且對主鍵等的屬性賦值好了)附加到上下文,然后標記狀態(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è):

Entity Framework之DB First方式有什么用

專業(yè)列表:

Entity Framework之DB First方式有什么用

同樣實現(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ù)錯誤");
      }
      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é)生時,報錯如下:

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的兩種更新方式。

關(guān)于“Entity Framework之DB First方式有什么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)頁題目:EntityFramework之DBFirst方式有什么用-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://chinadenli.net/article8/ejpip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站改版、面包屑導(dǎo)航定制開發(fā)、網(wǎng)站設(shè)計App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
国产精品欧美一区二区三区不卡| 色无极东京热男人的天堂| 中文字幕日韩一区二区不卡| 久热青青草视频在线观看| 中文字幕久热精品视频在线| 日韩av生活片一区二区三区| 日本精品视频一二三区| 久久精品国产一区久久久| 99久久国产精品免费| 亚洲综合色在线视频香蕉视频| 国产一级精品色特级色国产| 亚洲欧美日本视频一区二区| 日韩国产精品激情一区| 国产不卡一区二区四区| 精品午夜福利无人区乱码| 黄色美女日本的美女日人| 熟女一区二区三区国产| 日韩精品第一区二区三区| 精品国产亚洲区久久露脸| 中文字幕乱码一区二区三区四区| 视频一区二区 国产精品| 91播色在线免费播放| 中文字幕精品人妻一区| 国产国产精品精品在线| 国产精品免费视频视频| 国产av大片一区二区三区 | 国产肥妇一区二区熟女精品| 免费性欧美重口味黄色| 国产精品国产亚洲区久久| 国产精品久久女同磨豆腐| 免费一区二区三区少妇| 日韩免费成人福利在线| 欧美日韩人妻中文一区二区| 日韩中文字幕视频在线高清版 | 国产一区欧美午夜福利| 亚洲黑人精品一区二区欧美| 精品熟女少妇av免费久久野外| 国产成人综合亚洲欧美日韩| 欧美日韩一区二区午夜| 欧美做爰猛烈叫床大尺度| 国产av熟女一区二区三区蜜桃 |