net Core 通過 Ef Core 訪問、管理Mysql

來源:互聯網
上載者:User

標籤:

net Core 通過 Ef Core 訪問、管理Mysql

本文地址:http://www.cnblogs.com/likeli/p/5910524.html

環境

dotnet Core版本:1.0.0-preview2-003131

本文分為Window環境和Mac Os X環境。

相關資源下載

Visual Studio Code:https://code.visualstudio.com

DotNet Core:https://dotnet.github.io/

MySql.Data.EntityFrameworkCore:http://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.5-IR21

Entity Framework Core(Pomelo):https://docs.efproject.net/en/latest/providers/pomelo/index.html#supported-database-engines

Entity Framework Core(Official):https://docs.efproject.net/en/latest/providers/mysql/

MySql資料庫

安裝配置就不介紹了,不是本文重點,本文中,Mac下用的Mysql是通過brew包管理器直接安裝的。

登入資料庫檢查目前資料庫的內容情況

OS X環境

安裝DotNet Core SDK包和Visual Studio Code安裝包,上面的資源裡面有了,若是不知道如何配置,請看我另兩篇的文章,裡面做了介紹:http://www.cnblogs.com/likeli/p/5883551.html、http://www.cnblogs.com/likeli/p/5910475.html

建立Web項目

在準備的項目目錄下執行命令,進行建立:

dotnet new -t web

Visual Studio Code 開啟,安裝好了C#外掛程式後,可以著色、提示等。

匯入Mysql驅動包

開啟遊覽器,去NuGet逛逛,搜尋mysql官方出的驅動包 MySql.Data.EntityFrameworkCore

可以看到上面的庫名 MySql.Data.EntityFrameworkCore, 還有版本號碼 7.0.5-IR21

開啟項目中的project.json,在該檔案中加入mysql驅動包的名字,還有版本號碼。

然後在終端中鍵入命令,通過Nuget回複所有的依賴包:

dotbet restore

增加實體和上下文

在項目的Models檔案夾下建立新的.cs檔案,加入User.csBlog.cs

代碼如下:

User.cs

namespace WebApplication.Models{    public class User    {        public int UserId { set; get; }        public string Name{set;get;}    }}

Blog.cs

namespace WebApplication.Models{    public class Blog{        public int Id{set;get;}        public string Title{set;get;}        public string Content{set;get;}                public int UserId{set;get;}        public virtual User User{set;get;}    }}

在Data檔案夾下添加上下文:

DataContext.cs

using Microsoft.EntityFrameworkCore;using MySQL.Data.EntityFrameworkCore.Extensions;using WebApplication.Models;namespace WebApplication.Data{    public class DataContext : DbContext{        public DbSet<User> Users{set;get;}        public DbSet<Blog> Blogs{set;get;}        protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder)            => optionsBuilder.UseMySQL(@"Server=localhost;database=ef;uid=root;pwd=root");    }}

有過Ef使用經驗的開發人員應該很容易懂這個過程了,其實需要說明一下的就是在非Visual Studio中開發,不能直接使用NuGet的命令的時候,就直接在project.json裡面添加需要引用的包,然後執行dotnet restore還原包就可以了。

然後在Home控制器裡面寫調用代碼:

using(var context = new DataContext()){       context.Database.EnsureCreated();       var user = new User {Name="憤怒的TryCatch"};       context.Add(user);          context.SaveChanges();}
編譯測試

在終端鍵入 dotnet build 命令進行編譯,然後‘dotnet run‘運行。

查看一下資料庫裡面的變化:

可以看到MySQL資料裡面多出了一個名叫Ef的庫

表結構也已經建立,User表中已經添加了新資料。

現在加上Blog的資料,這裡表現外鍵關係。

發現外鍵關係也有了。

總結 目前看來,Ef Core 的使用方法和Windows上的EF差別並不大,但是目前只是早期版本,版本應該還會快速迭代,用於生產環節,那就得請三思了。
本文是在Mac上做了了Ef Core的操作示範,我也在Windows上測試過了,編寫方法和方式都是一樣的。各位類推一下就可以了。

net Core 通過 Ef Core 訪問、管理Mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.