1024programmer Asp.Net .NET6 + EF Core + MySQL create entities and databases, EFCore data migration

.NET6 + EF Core + MySQL create entities and databases, EFCore data migration

.NET6 + EF Core + MySQL create entities and databases, EFCore data migration

Foreword

Continuing from the previous article “Methods and Methods of Connecting Databases for .NET6 Projects”, someone asked me a few questions. Now I will expand on these questions to create entity classes and databases. Include the ORM framework and data migration.

Install ORM framework, here we use EFCore

Install EFCore

My project was created on Linux and developed remotely using the vscode development tool. In order to facilitate everyone’s reading and operation, I downloaded the project to my local computer (Windows 10 system) and developed it using the professional .NET development tool Visual Studio.
Insert image description here

Create entity class


The solution after adding is like this
The contents of the User.cs class are as follows:

using Microsoft.EntityFrameworkCore.Metadata.Internal;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;

 namespace test.Models
 {
     public class User
     {
         [Key] //Database primary key
         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; }

     }
 }
 

Create data context class

Add the data context folder MyDataBaseContext, the solution structure after adding is as follows

Insert image description here

Create the data context MyDataBaseContext_main.cs of the operation main library and inherit the data context DbContext

The actions to operate the main library include adding, modifying and deleting, as shown in the figure:
Insert image description here

The code is as follows:

using Microsoft.EntityFrameworkCore;
 using test.Models;

 namespace test.MyDataBaseContext
 {
     public class MyDataBaseContext_mian : DbContext
     {
         //Add User class
         public DbSet Users { get; set; }
         //Constructor
         public MyDataBaseContext_mian(DbContextOptions option) : base(option)
         {

         }
     }
 }
 

Create the data context MyDataBaseContext_from.cs of the operation slave library and inherit the data context DbContext

The only action to operate the main database is query, as shown in the figure:
Insert image description here
The code is as follows:

using Microsoft.EntityFrameworkCore;
 using test.Models;

 namespace test.MyDataBaseContext
 {
     public class MyDataBaseContext_from : DbContext
     {
         //Add User class
         public DbSet Users { get; set; }
         //Constructor
         public MyDataBaseContext_from(DbContextOptions option) : base(option)
         {

         }
     }
 }
 

Add connection string configuration

According to the method in the previous article “How to connect to the database for .NET6 project”, first add the database connection string 2 in the appsettings file to make it easier for everyone to distinguish. It is written like this:
Insert image description here

"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"
   }
 

Secondly, register the service in the Program.cs file. It should be noted that we need to register two data contexts. Only in this way can the separation of reading and writing be achieved, as shown in the figure:
Insert image description here
The code is as follows:

//Register the data context of the main library
 builder.Services.AddDbContext(
     options =>
     {
         options.UseMySql(builder.Configuration.GetConnectionString("MySqlDataBase"), new MySqlServerVersion(new Version(8, 0, 31)));
     });
 //Register the data context of the slave library
 builder.Services.AddDbContext(
     options =>
     {
         options.UseMySql(builder.Configuration.GetConnectionString("MySqlDataBase2"), new MySqlServerVersion(new Version(8, 0, 31)));
     });
 

Start migration

Search and install the following two packages on NuGet package, Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
Insert image description here
To start the migration, click Tools, NuGet Package Manager, Package Manager Console, and open the console. Because we have two DbConexts, using the Add-Migration command directly will report the following error.
Insert image description here
Therefore, we need to operate separately. The operation method is as follows:
First operate the context of the main library:

# Data migration
 add-migration testDataBaseMigraMain -c MyDataBaseContext_mian -o test/DataMigra/main
 #Update to database
 Update-Database -Context MyDataBaseContext_mian
 

Secondly operate the context of the slave library:

# Data migration
 add-migration testDataBaseMigraFrom -c MyDataBaseContext_from -o test/DataMigra/from
 #Update to database
 Update-Database -Context MyDataBaseContext_from
 

Annotations: -c/-Context: Which DbConext; -o: This DbConext corresponds to the directory corresponding to the generated Migrations file
Insert image description here
Insert image description here
Insert image description here
You can see that two databases have been generated in MySQL, and the tables in each database are the same.

Configuration database master-slave synchronization

Database master-slave synchronization will not be demonstrated here. Friends who don’t know how to do it can read my other article “MySQL 8.0 Database Data Synchronization Based on Canal”. Link address

Next issue preview:
Basic addition, deletion, modification and query of EF Core operation database under the .net 6 framework

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/net6-ef-core-mysql-create-entities-and-databases-efcore-data-migration/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索