Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call

  • A+
所属分类:.NET技术
摘要

Consider enabling transient error resiliency by adding ‘EnableRetryOnFailure()’ to the ‘UseMySql’ call

Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call

ef core连接不上MySQL

连接字符串是:

'Data Source = 127.0.0.1;Database = test1;UserID=root;password=123456;pooling=true;port=3306;sslmode=none;Charset=utf-8';

乍一看没啥错

 正确的:'Data Source = 127.0.0.1;Database = 'test1';UserID=root;password=123456;pooling=true;port=3306;sslmode=none;Charset=utf-8';

数据库名要加引号

 

 

=====================

连接MySql(基于.net core3.1)

首先第一步安装包,在NuGet包管理器里面安装Pomelo.EntityFrameworkCore.MySql

nuget慢的话可以使用命令行:PM>dotnet add package Pomelo.EntityFrameworkCore.MySql

第二步在配置appsettings配置文件中配置连接信息:

{   "Logging": {     "IncludeScopes": false,     "LogLevel": {       "Default": "Warning"     }   },   "ConnectionStrings": { "MysqlConnection": "Data Source=localhost;Database=test;User ID=root;Password=123456;pooling=true;CharSet=utf8;port=3306;sslmode=none" }  }  //Data Soruce:IP //Database:数据库名 //User ID:数据库用户名 后面的都很容易看懂

第三步在Startup.cs文件的ConfigureServices方法添加:

 var connection = Configuration.GetConnectionString("MysqlConnection");  //这里的MysqlConnection要和appsettings中ConnectionStrings里面的MysqlConnection相同 services.AddContextPool<AppDbContext>(options =>      //这是我目前找到的唯一一种在ConfigureServices里面配置Mysql服务     options.UseMySql(         connection,         new MySqlServerVersion(new SyStem.Version(8,0,17))       ) ); //网上很多都是 services.AddDbContext<DBContext>(options => options.UseMySQL(connection)); //但是这种不知道.net 3.1不支持,UseMySQL方法里面非空的参数没有只有connection这一个参数的重载方法。

迁移

在程序包管理控制台中执行

Install-Package Microsoft.EntityFrameworkCore.Tools