欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-28 23:31:09
1.使用nuget下载:microsoft.extensions.configuration.jsonpomelo.entityframeworkcore.mysql2.数据库中创建好表:create...

1.使用nuget下载:

microsoft.extensions.configuration.json

pomelo.entityframeworkcore.mysql

2.数据库中创建好表:

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.新建实体类:

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,设置为始终复制。

{
  "connectionstrings": {
    "default": "server=127.0.0.1;database=wdb;charset=utf8;uid=root;pwd=some;"
  }
}

5.新建dbcontext

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.使用

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: .NET5 EF MYSQL