Netcore mvc efcore simple framework construction + addition, deletion, modification and query
The database used in this example is mysql; .net core framework, version (sdk) is 3.1.
One: Create a net core version of mvc
Target framework selection net core3.1
2: After the project is created, first install the nuget package you will use today (the downloaded package should preferably be version 3.1)
Microsoft.EntityFrameworkCore;
Microsoft.EntityFrameworkCore.Design;
Microsoft.EntityFrameworkCore.Tools;
Pomelo.EntityFrameworkCore.MySql;
For example: (You can manually install using the nuget interface, or you can write statement installation)
Three: Overall configuration
1. The database connection is in appsettings.json
//Connect to database "AppConfig": { "AesKey": "11*222sdf52sf2f2fsdf/.*222,ssddd", "DbConn span>": "Server=localhost;port=3306;database=user;uid=root;pwd=root;Convert Zero Datetime=True; " }
2. Create a class: AppConfig
public classAppConfig { ////// Aes key /// public string AesKey { get; set; } /// /// Database connection string /// public string DbConn { get; set; } }
3. Add a class: NetcoremvcDbcontext as the context and inherit: DbContext class
public class NetcoremvcDbcontext : DbContext { private string span> _connectionString; public NetcoremvcDbcontext(IOptionsSnapshot options) { _connectionString = options.Value.DbConn; } protected override voidOnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connectionString = _connectionString ; //Connect to the database UseMySql keyword //MaxBatchSize one An integer value indicating the maximum number of pages that will be compiled into a single batch. The default number of pages is 1000. (The current maximum processing capacity is 30 items) optionsBuilder.UseMySql(connectionString, b => b.MaxBatchSize(30)); } public DbSet User { get; set; } }
Four: Create an entity class
public class User { public string span> Id { get; set; } public string span> Name { get; set; } public string span> Phone { get; set; } public int span> Age { get; set; } public string span> Like { get; set; } }
Five: Create a new view controller with a template
1. Create a new controller
2. Select your entity
Six: File startup configuration
Because the startup item program points to startup by default, the configuration is written in startup
First add it to the ConfigureServices method of the startup file
services.AddDbContext(); services.Configure(Configuration.GetSection("AppConfig"));
Replace the route in the Configure method in startup with the default route of your own project
Seven: Connect to database and model migration
1. Find the package management background and enter the add statement: Add-Migration init1 to add a new migration
After success, a folder named Migrations will appear in the project
Indicates that the generation was successful
2. Update statement: Update-Database
The update statement updates the database to the specified migration
Check that the tables in the database have been created
The controllers and views created using this mvc view template can automatically generate simple add, delete, modify and check methods. Now see if your newly created controller has basic add, delete, modify and check. You can also run the project to see Effect
The following is my rendering
The object-relational mapping in this article only uses the simplest one. For the one that I think is most suitable and useful, please check the article: EF Core object-relational mapping multiple methods
This example is very simple and it is recommended to write it yourself.
Baidu Skydisk
Link: https://pan.baidu.com/s/1g0F3jH2aou9Ka7X0ulKhHg
Extraction code: dtsp
g/2697105/202304/2697105-20230422005754959-280783301.png” alt=”” loading=”lazy”>
The controllers and views created using this mvc view template can automatically generate simple add, delete, modify and check methods. Now see if your newly created controller has basic add, delete, modify and check. You can also run the project to see Effect
The following is my rendering
The object-relational mapping in this article only uses the simplest one. For the one that I think is most suitable and useful, please check the article: EF Core object-relational mapping multiple methods
This example is very simple and it is recommended to write it yourself.
Baidu Skydisk
Link: https://pan.baidu.com/s/1g0F3jH2aou9Ka7X0ulKhHg
Extraction code: dtsp