Question

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:

enter image description here

and it worked.

So I must miss some settings with UseSqlServer..

(I'm following this tutorial)

1
2
4/1/2018 9:12:45 AM

Accepted Answer

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;");
}
3
3/31/2018 3:46:28 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow