Here is my connection string:
Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;
That I use like this:
services.AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseUrl")));
And I can't connect to my database, I got this error:
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)
Thus I tried with HeidiSQL, as follows:
and it worked.
So I must miss some settings with UseSqlServer
..
(I'm following this tutorial)
System.Data.SqlClient.SqlException
is for Microsoft SQL Server - but it will NOT work for MySQL - those are two completely different database systems!
You need to use the appropriate, MySQL-specific classes!
Check out this page: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html
There is a MySQL connector for Entity Framework Core, and you can use this by specifying:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
.....
optionsBuilder.UseMySQL("Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;");
}
As @marc_s pointed out, you can override OnConfiguring
to use MySQL.
Or if you wish a cleaner & more "pattern-friendly" way: check this out