C# .Net webapi NET6

83 阅读1分钟

C# .Net

eftool安装 dotnet tool install -g dotnet-ef

生成数据库迁移 dotnet ef migrations add init

数据库更新 dotnet ef database update

appsetting.json { "ConnectionStrings": { "MySQLConnection": "server=localhost;port=3306;uid=root;pwd=root;database=EFCoredb" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" }

program.cs //mysql链接 builder.Services.AddDbContext(opt => { string connectionString = builder.Configuration.GetConnectionString("MySQLConnection"); var serverVersion = ServerVersion.AutoDetect(connectionString); opt.UseMySql(connectionString, serverVersion); });