ASP.NET核心API : How to use MySQL

标签 asp.net asp.net-web-api entity-framework-core asp.net-core-2.0 asp.net-core-webapi

这是我的连接字符串:

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): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

System.ComponentModel.Win32Exception (0x80004005): The parameter is incorrect

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

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

enter image description here

并且成功了。

所以我一定错过了 UseSqlServer 的一些设置..

(我正在关注 this tutorial )

最佳答案

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;");
}

关于ASP.NET核心API : How to use MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588770/

相关文章:

c# - 带有异步 Ajax 请求的同步 ActionFilter

c# - 使用本地帐户使用安全的 ASP Net 5 web api

c# - .net 核心 2.2 EF : SaveChangesAsync() setting unrelated columns to null

c# - 在多个解耦系统之间进行通信

c# - 如何从 EF7 DbContext 获取 ConnectionString

c# - EF Core 在迁移 (UP) 时将列数据从表移动到另一个表

html - 如何为具有相同分辨率的不同设备提供相同网址的不同 View ?

asp.net - ext.net 中的客户端验证和显示字段样式

ñÑ 字符的 Javascript 验证适用于 Chrome,但不适用于 Firefox

asp.net-mvc-5 - Web API 2 - 阻止所有外部调用