Net core how to operate MySQL database with ORM



It is well known that MySQL database is small in size, fast in speed and low in total cost of ownership, especially in open source. In order to reduce the total cost of ownership of websites, many small and medium-sized websites choose MySQL as their website database. MySQL is a multi-user, multi-threaded relational database management system. The working mode is based on client / server structure. At present, it can support almost all operating systems.

In short, MySQL is an open, fast, multi-threaded, multi-user SQL database server.

Next, I’ll explain how to use MySQL database in. Net core and share it for your reference and study. I won’t say much. Let’s take a look at the detailed introduction.

The operation method is as follows:

Create a new web project, and then reference the MySQL related ORM assembly:


Then create the corresponding database and data table in MySQL:


To add a data table entity to a project:

public class t_test
  public int id { get; set; }
  public string name { get; set; }

Then add the database context:

/// <summary>
 ///Database context
 /// </summary>
 public class DataContext : DbContext
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   //Configure sqlserver connection string
   optionsBuilder.UseMySql("Server=;Database=test; User=root;Password=123456;");
  //Add table entity
  public DbSet<t_test> t_test { get; set; }

Add the following script to the home controller index:

public IActionResult Index()
   DataContext context = new DataContext();
   string name = context.t_test.Find(1).name;
   return Content(name);

Run the project to see the output:


As you can see, the results output the corresponding data in the database, so far, the function of operating MySQL database in. Net core has been realized.


The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.