服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - .NET5控制台程序使用EF连接MYSQL数据库的方法

.NET5控制台程序使用EF连接MYSQL数据库的方法

2021-12-16 14:43runliuv ASP.NET教程

这篇文章主要介绍了.NET5控制台程序使用EF连接MYSQL数据库,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1.使用nuget下载:

Microsoft.Extensions.Configuration.Json

Pomelo.EntityFrameworkCore.MySql

2.数据库中创建好表:

?
1
2
3
4
5
6
CREATE TABLE `user2` (
  `mid` int NOT NULL AUTO_INCREMENT,
  `uname` varchar(45) NOT NULL,
  `memo` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

3.新建实体类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
 
namespace ConsoleApp1
{
    public class user2
    {
        [Key] //主键
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  //设置自增
        public int mid { get; set; }
        public string uname { get; set; }
 
        public string memo { get; set; }
    }
}

4.新建一个json配置文件:appsettings.json,设置为始终复制。

?
1
2
3
4
5
{
  "ConnectionStrings": {
    "Default": "Server=127.0.0.1;Database=wdb;charset=utf8;uid=root;pwd=some;"
  }
}

5.新建DbContext

?
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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.IO;
 
namespace ConsoleApp1
{
    public class DefaultDbContext : DbContext
    {         
 
        private IConfiguration configuration;
 
        public DefaultDbContext()
        {            
            configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build();
        }
 
        public DbSet<user2> user2 { get; set; }
 
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connStr = configuration.GetConnectionString("Default");
            optionsBuilder.UseMySql(connStr, ServerVersion.AutoDetect(connStr),null);
        }       
    }
}

6.使用

?
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
32
33
34
35
36
37
38
using ConsoleApp1;
using System;
using System.Linq;
 
namespace Net5ConsoleMySql
{
    class Program
    {
        static void Main(string[] args)
        {           
            try
            {
                DefaultDbContext context = new DefaultDbContext();
                Random rnd = new Random();
                string i = rnd.Next(1000, 9000).ToString();
                user2 zhangsan = new user2 { uname = "张三" + i, memo = i };
                user2 lisi = new user2 { uname = "李四" + i, memo = i };
                context.user2.AddRange(zhangsan, lisi);
                context.SaveChanges();
 
                var users = context.user2.ToList();
                foreach (var user in users)
                {
                    Console.WriteLine($"{user.mid} {user.uname} {user.memo}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"EX: {ex.Message}  ");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"INNER EX: {ex.InnerException.Message}  ");
                }
            }
            Console.WriteLine("Hello World!");
        }
    }
}

到此这篇关于.NET5控制台程序使用EF连接MYSQL数据库的方法的文章就介绍到这了,更多相关.NET5使用EF连接MYSQL内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/runliuv/p/15107430.html

延伸 · 阅读

精彩推荐