ASP.NET vNext總結:EntityFramework7

來源:互聯網
上載者:User

標籤:

1.概述

      關於EF7之前的版本如何?這裡就不再扯了。更不會和別人爭論EF的效能如何?好比一把寶刀,在善於用它的高手和不善於用它的低手使用威力不同的。

      如題,這裡簡單說一下EF7,同樣它還處於beta版本階段,如果你遇到bug或功能變化等疑問,不要見怪了。

      EF7隻支援CodeFrist代碼優先的映射方式,將來它可以支援更多種資料庫,如已經對SQLLite嵌入式資料庫支援很好,甚至牛逼得還要支援NoSQL(非關係型資料庫)。當然,我也不會說別的ORM就Out,Old了。一樣東西之所以存在並有部分人使用,必然有它的價值,從哲學上來說,存在即合理!

      EF只是封裝.Net操作和訪問資料庫(也就是在ADO.NET基礎上),但你首先把它當成映射工具,這也是它的出現第一初衷解決面對對象模型和關係型資料模型映射,至於你非要在意效能,你可以EF中使用原始的SQL語句及預存程序和ADO.NET底層技術。

      顯然EF7不是專屬於ASP.NET vNext中使用,但本介紹是專題系列,體驗在vNext項目模板中使用。

2.建立項目

      在前面文章介紹過的”vNext“解決方案上,右鍵:添加-》建立項目-》Visual C#-》ASP.NET 5 Console Application,其項目“名稱”:vNext.ConsoleAppTemplate,並把它”設為項目啟動項“。

      這裡先以vNext模板的控制台項目介紹,在後面完整的項目實戰中,再在Web項目中“研究”!

3.安裝EF及設定commands

     開啟project.json:

    

     註:commands的ef配置是啟動ef遷移用的,後面也會說明的。

4.添加模型類及上下文類

      先項目根目錄下,建立Models檔案夾,在其裡面添加三個類檔案:

     

      Role.cs代碼:

using System.Collections.Generic;namespace vNext.ConsoleAppTemplate.Models{    /// <summary>    /// 角色實體類    /// </summary>    public class Role    {        public int ID { get; set; }        public string Name { get; set; }        //導覽屬性        public ICollection<User> Users { get; set; }    }}

      User.cs代碼:

namespace vNext.ConsoleAppTemplate.Models{    /// <summary>    /// 使用者實體類    /// </summary>    public class User    {        public int ID { get; set; }        public string Name { get; set; }        public Gender Gender { get; set; }        //外鍵屬性        public int RoleID { get; set; }        //導覽屬性        public Role Role { get; set; }    }    /// <summary>    /// 性別 枚舉類型    /// </summary>    public enum Gender { man, woman, secrecy }}

      EFContext.cs代碼:

using Microsoft.Data.Entity;using Microsoft.Data.Entity.Metadata;namespace vNext.ConsoleAppTemplate.Models{    /// <summary>    /// 資料庫上文類    /// </summary>    public class EFDbContext : DbContext    {        public DbSet<Role> Roles { get; set; }        public DbSet<User> Users { get; set; }        protected override void OnConfiguring(DbContextOptions options)        {            //資料庫連接字串            options.UseSqlServer("Server=.;Database=TestDB;UID=sa;PWD=123456");        }        protected override void OnModelCreating(ModelBuilder modelBuilder)        {            //多對一關聯性及指定外鍵            //EF7好像沒法自動關係定義,所以這裡定義一下            modelBuilder.Entity<User>().ManyToOne(r => r.Role, u => u.Users).ForeignKey(f => f.RoleID);        }    }}

5.遷移

      開啟vs,工具-》Nuget封裝管理員-》封裝管理員控制台:

      第一步:當前項目路徑

     

      第二步:啟用ef commands

     

      註:這一步必須在project.json中commands配置ef。

     第三步:添加遷移

    

     這一步ok,會出現:

    

     第四步:同意遷移

    

     第五步:檢查資料庫

     

     也可以看一下關係圖:

     

6.小結

      本章算是小試牛刀開個頭了,後面在Web項目中再繼續EF的GRUD(增刪改查)。下一章介紹項目發布和部署!   

 

ASP.NET vNext總結:EntityFramework7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.