前言
接上期文章《.NET6項(xiàng)目連接數(shù)據(jù)庫(kù)方式方法》,有人問(wèn)了我?guī)讉€(gè)問(wèn)題,現(xiàn)在就這幾個(gè)問(wèn)題,拓展延申一下創(chuàng)建實(shí)體類、數(shù)據(jù)庫(kù)。把ORM框架和數(shù)據(jù)遷移都寫進(jìn)去。
安裝ORM框架,這里我們采用EFCore
安裝EFCore
我的項(xiàng)目是在Linux上創(chuàng)建的,使用的是vscode開(kāi)發(fā)工具遠(yuǎn)程開(kāi)發(fā)。為了方便大家閱讀和操作,我將項(xiàng)目down到我的本地電腦(Windows10系統(tǒng)),使用專業(yè)的.NET開(kāi)發(fā)工具Visual Studio開(kāi)發(fā)。
創(chuàng)建實(shí)體類
添加完以后的解決方案是這樣的
User.cs類的內(nèi)容如下:
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace test.Models
{
public class User
{
[Key] //數(shù)據(jù)庫(kù)主鍵
public int UserId { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string UserName { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string UserPwd { get; set; }
public int UserAge { get; set; }
[Column(TypeName = "nvarchar(200)")]
public string? UserAddress { get; set; }
}
}
創(chuàng)建數(shù)據(jù)上下文類
添加數(shù)據(jù)上下文文件夾MyDataBaseContext,添加以后解決方案結(jié)構(gòu)如下
創(chuàng)建操作主庫(kù)的數(shù)據(jù)上下文MyDataBaseContext_main.cs并繼承數(shù)據(jù)上下文DbContext
操作主庫(kù)的動(dòng)作有添加、修改、刪除,如圖所示:
代碼如下:
using Microsoft.EntityFrameworkCore;
using test.Models;
namespace test.MyDataBaseContext
{
public class MyDataBaseContext_mian : DbContext
{
//添加User類
public DbSet<User> Users { get; set; }
//構(gòu)造函數(shù)
public MyDataBaseContext_mian(DbContextOptions<MyDataBaseContext_mian> option) : base(option)
{
}
}
}
創(chuàng)建操作從庫(kù)的數(shù)據(jù)上下文MyDataBaseContext_from.cs并繼承數(shù)據(jù)上下文DbContext
操作主庫(kù)的動(dòng)作只有查詢,如圖所示:
代碼如下:
using Microsoft.EntityFrameworkCore;
using test.Models;
namespace test.MyDataBaseContext
{
public class MyDataBaseContext_from : DbContext
{
//添加User類
public DbSet<User> Users { get; set; }
//構(gòu)造函數(shù)
public MyDataBaseContext_from(DbContextOptions<MyDataBaseContext_from> option) : base(option)
{
}
}
}
添加連接字符串配置
按照上一篇文章《.NET6項(xiàng)目連接數(shù)據(jù)庫(kù)方式方法》的方式方法首先在appsettings文件中添加連接數(shù)據(jù)庫(kù)字符串二,便于大家區(qū)分,寫成這樣:
"ConnectionStrings": {
"MySqlDataBase": "Server=192.168.11.82;Port=3306;User Id=ymliu;Password=ymliu2023;Database=BlogDataBase",
"MySqlDataBase2": "Server=192.168.11.82;Port=3306;User Id=ymliu;Password=ymliu2023;Database=BlogDataBase2"
}
其次在Program.cs文件中注冊(cè)服務(wù),需要注意的是,我們需要注冊(cè)兩個(gè)數(shù)據(jù)上下文。才能實(shí)現(xiàn)讀寫分離,如圖所示:
代碼如下:
//注冊(cè)操作主庫(kù)的數(shù)據(jù)上下文
builder.Services.AddDbContext<MyDataBaseContext_mian>(
options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("MySqlDataBase"), new MySqlServerVersion(new Version(8, 0, 31)));
});
//注冊(cè)操作從庫(kù)的數(shù)據(jù)上下文
builder.Services.AddDbContext<MyDataBaseContext_from>(
options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("MySqlDataBase2"), new MySqlServerVersion(new Version(8, 0, 31)));
});
開(kāi)始遷移
在NuGet程序包上搜索安裝下面這兩個(gè)包,Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
開(kāi)始遷移,點(diǎn)擊工具,NuGet包管理器,程序包管理器控制臺(tái),打開(kāi)控制臺(tái)。因?yàn)槲覀冇袃蓚€(gè)DbConext,直接使用Add-Migration命令會(huì)報(bào)如下錯(cuò)誤。
因此我們需要分開(kāi)操作,操作方式如下:
首先操作主庫(kù)的上下文:
# 數(shù)據(jù)遷移
add-migration testDataBaseMigraMain -c MyDataBaseContext_mian -o test/DataMigra/main
# 更新到數(shù)據(jù)庫(kù)
Update-Database -Context MyDataBaseContext_mian
其次操作從庫(kù)的上下文:
# 數(shù)據(jù)遷移
add-migration testDataBaseMigraFrom -c MyDataBaseContext_from -o test/DataMigra/from
# 更新到數(shù)據(jù)庫(kù)
Update-Database -Context MyDataBaseContext_from
注解: -c/-Context :哪個(gè)DbConext ;-o :這個(gè)DbConext對(duì)應(yīng)生成 的Migrations文件對(duì)應(yīng)的目錄
可以看到MySQL中已經(jīng)生成兩個(gè)數(shù)據(jù)庫(kù),每個(gè)數(shù)據(jù)庫(kù)中的表都是一樣的。
配置數(shù)據(jù)庫(kù)主從同步
數(shù)據(jù)庫(kù)主從同步,這里不再演示,不會(huì)的小伙伴可以去看我的另外一篇文章《基于Canal實(shí)現(xiàn)MySQL 8.0 數(shù)據(jù)庫(kù)數(shù)據(jù)同步》。鏈接地址文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-449135.html
下期預(yù)告:
.net 6框架下的EF Core操作數(shù)據(jù)庫(kù)基本增刪改查文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-449135.html
到了這里,關(guān)于.NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫(kù)、EFCore 數(shù)據(jù)遷移的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!