ASP.NET Core API:如何使用MySQL

亚历山大·杜布里考特

这是我的连接字符串:

Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;

我这样使用:

services.AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseUrl")));

而且我无法连接到数据库,出现此错误:

System.Data.SqlClient.SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。(提供者:SQL网络接口,错误:25-连接字符串无效)

System.ComponentModel.Win32Exception(0x80004005):参数不正确

在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔值allowCreate,仅布尔值OneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)

因此,我尝试使用HeidiSQL,如下所示:

在此处输入图片说明

而且有效。

所以我必须错过一些UseSqlServer..

(我正在学习本教程

marc_s

System.Data.SqlClient.SqlException适用于Microsoft SQL Server-不适用于MySQL-这是两个完全不同的数据库系统!

您需要使用适当的特定于MySQL的类!

签出此页面:https : //dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html

有一个用于Entity Framework Core的MySQL连接器,您可以通过指定以下内容来使用它:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
   .....         
   optionsBuilder.UseMySQL("Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章