这是我的连接字符串:
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,如下:
并且成功了。
所以我一定错过了 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/