一.代碼優先 創建數據庫(SQLServer2012)
1.創建core3.1的項目
項目結構如下
如圖,我這里創建了一個core的類庫用來保存數據庫相關的實體
注意:使用的類庫環境必須與core項目的環境一致
2.添加所需NUGet包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
3.創建一些實體
添加實體注解需要引用命名空間:using System.ComponentModel.DataAnnotations;
3.1用戶類
public class Customers { public int ID { get; set; } [StringLength(50)] public string Name { get; set; } [StringLength(2)] public string Sex { get; set; } [StringLength(11)] public string Phone { get; set; } [StringLength(200)] public string Address { get; set; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3.2商品類
public class Product { public int ID { get; set; } [StringLength(20)] public string Name { get; set; } public decimal Price { get; set; } public string Desc { get; set; } /// <summary> /// 導航屬性 一件商品對應一個分類 /// </summary> public ProductType ProductType { get; set; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3.3商品類型類
public class ProductType { public int ID { get; set; } [StringLength(20)] public string TypeName { get; set; } /// <summary> /// 導航屬性 一對多,一個類型可對多個商品 /// </summary> public ICollection<Product> Products { get; set; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.4上下文類
/// <summary> /// 上下文類 /// 繼承系統上下文 /// </summary> public class CoreMVCContext:DbContext { public CoreMVCContext() { } public CoreMVCContext(DbContextOptions option) : base(option) { } public DbSet<ProductType> ProductTypes { set; get; } public DbSet<Product> Product { set; get; } public DbSet<Customers> Customers { set; get; } /// <summary> /// 重寫父類的方法 用於連接數據庫 /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { //連接字符串 optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=CoreMVC;Integrated Security=True"); } } }
- 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
4.准備代碼的遷移與數據庫的更新
4.1代碼遷移:Add-Migration
4.2數據庫更新:update-database
二、數據庫優先 創建實體類(SQLServer2012)
1.獲得數據庫的連接字符串
在這里右鍵添加連接,選擇需要連接的服務器和數據庫,本地為‘.’,,測試連接,連接成功了去“高級”里面將連接字符串復制出來,等會要用
2.安裝所需依賴,同上
3.連接數據庫
Scaffold-DbContext "Data Source=.;Initial Catalog=UU;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context UUDBContext
若啟動項目不對
4.成功
有警告,注釋掉那一行即可
三、數據庫優先 創建實體類(MySQL 5.7)
本以為連接mysql會麻煩不少,記得年初連接mysql時還不支持ef6.x的框架,要連接還需要使用低配的依賴包才可以,但是這次卻直接可以了,感覺有點神奇
1.獲得連接字符串
同上,如果是知道連接的可以跳過
2.安裝依賴包
因為這是mysql的,使用依賴包和上面的有所不同Microsoft.EntityFrameworkCore.Tools
Microsoft.VisualStudio.Web.CodeGeneration.Design
MySql.Data.EntityFrameworkCore
Pomelo.EntityFrameworkCore.MySql
3.連接數據庫並遷移
NuGet包控制台:Scaffold-DbContext "server=數據庫服務器;uid=數據庫用戶名;pwd=數據庫密碼;database=數據庫名;" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Force
.Net Core CLi:dotnet ef dbcontext scaffold "server=數據庫服務器;uid=數據庫用戶名;pwd=數據庫密碼;database=數據庫名;" Pomelo.EntityFrameworkCore.MySql -o Models -f
MySQL的代碼優先與上差不多
四、補充(代碼參數說明)
補充:其它數據庫提供程序請參考:https://docs.microsoft.com/zh-cn/ef/core/providers/
代碼參數說明:
-OutputDir (-o) *** 實體文件所存放的文件目錄
-ContextDir *** DbContext文件存放的目錄
-Context *** DbContext文件名
-Schemas *** 需要生成實體數據的數據表所在的模式
-Tables(-t) *** 需要生成實體數據的數據表的集合
-DataAnnotations
-UseDatabaseNames 直接使用數據庫中的表名和列名(某些版本不支持)
-Force (-f) 強制執行,重寫已經存在的實體文件
五、更新
如果出現如下錯誤:
CS1705 C# 標識為“ShopMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”的程序集“ShopMode”所使用的“Microsoft.EntityFrameworkCore, Version=3.1.7.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”版本高於所引用的標識為“Microsoft.EntityFrameworkCore, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”的程序集“Microsoft.EntityFrameworkCore”
- 1
則將Microsoft.EntityFrameworkCore.Tools
的版本號進行降級
,降到3.1.0即可
錯誤原因是該依賴包的版本要比項目文件的版本高