c#实体框架连接Azure MySql数据库



我在Azure MySql数据库中有一个更新数据库的问题,我可以通过SSMS连接到我的数据库,没有问题,但当我尝试不更新数据库时,我得到错误

Entity Framework Core 6.0.15 initialized 'ApplicationDbContext' using prov
ider 'MySql.EntityFrameworkCore:6.0.10+MySQL8.0.32' with options: None
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of
the specified MySQL hosts.
---> System.Net.Sockets.SocketException (10051): Próba przeprowadzenia operacj
i, wykonywanej przez gniazdo, na nieosiÄ…galnej sieci.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(Soc
ketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.T
asks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(
Object state)
--- End of stack trace from previous location ---
at System.Net.Sockets.TcpClient.CompleteConnectAsync(Task task)
at MySql.Data.MySqlClient.NativeDriver.<>c.<Open>b__40_0(Exception ex)
at System.AggregateException.Handle(Func`2 predicate)
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings
)
at MySql.Data.Failover.FailoverManager.AttemptConnection(MySqlConnection conn
ection, String originalConnectionString, String& connectionString, Boolean mySql
PoolManager)
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.<>c__Displ
ayClass16_0.<Exists>b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClas
s12_0`2.<Execute>b__0(DbContext c, TState s)
at MySql.EntityFrameworkCore.Storage.Internal.MySQLExecutionStrategy.Execute[
TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,T
Result](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 veri
fySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,T
Result](IExecutionStrategy strategy, TState state, Func`2 operation)
at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.Exists(Boo
lean retryOnNotExists)
at MySql.EntityFrameworkCore.Storage.Internal.MySQLDatabaseCreator.Exists()
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String
targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateD
atabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(
String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c_
_DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execu
te(Action action)
Unable to connect to any of the specified MySQL hosts.

这是我的连接字符串

"con": "Data Source=tcp:amcia.database.windows.net,1433;Initial Catalog=amcia;User ID=gryTerAdmin@amcia;Password=<password>;Connection Timeout=30;" 

,我用它来连接到db在program.cs

builder.Services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
{
IConfiguration configuration = serviceProvider.GetService<IConfiguration>();
string connectionString = configuration.GetConnectionString("con");
options.UseMySQL(connectionString, mySqlOptions => { });
});

我关闭了PC上的防火墙,并在Azure上添加了我的IP,我使用。net 6.0, Pomelo。EntityFrameworkCore 6.0.0-rc。2、EntityFrameworkCore 6.0.15

我建议您修复您的连接字符串并使用服务器版本自动检测。试试这个

"con": "Server=amcia.database.windows.net;Uid=gryTerAdmin@amcia;Pwd=<password>;Database=amcia" 

builder.Services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
{
IConfiguration configuration = serviceProvider.GetService<IConfiguration>();
string connectionString = configuration.GetConnectionString("con");
options.UseMySQL(connectionString, ServerVersion.AutoDetect(connectionString)).EnableSensitiveDataLogging());
});

相关内容

  • 没有找到相关文章

最新更新