netcore数据连接配置
程序员文章站
2022-12-08 19:28:07
netcore数据连接配置 第一种 appsettings.json ConfigureServices 第二种 ConfigureServices ......
netcore数据连接配置
第一种
appsettings.json
{ "connectionstrings": { "defaultconnection": "server=localhost;database=typecho;uid=root;pwd=woshishui;" }, "logging": { "loglevel": { "default": "information", "microsoft": "warning", "microsoft.hosting.lifetime": "information" } }, "allowedhosts": "*" }
configureservices
 services.adddbcontext<typechocontext>(options=>options.usemysql(configuration.getconnectionstring("defaultconnection")));
第二种
configureservices
// other service configurations go here // replace "yourdbcontext" with the class name of your dbcontext services.adddbcontextpool<typechocontext>(options => options // // replace with your connection string .usemysql("server=localhost;database=typecho;user=root;password=woshishui;", mysqloptions => mysqloptions // // replace with your server version and type .serverversion(new serverversion(new version(8, 0, 19), servertype.mysql)) // ));