Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to connect to my MySql Workbench database in ASP.NET Core. I'm following a youtube guide where I'm told to write some stuff in the Package Manager Console:

Add-Migration "Initial-Create"

And then:

Update-Database

Now when I run the second command I get this:

Build started...
Build succeeded.
System.ArgumentException: Keyword not supported: 'port'.
   at Microsoft.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
   at Microsoft.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
   at Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Keyword not supported: 'port'.

My connectionstring looks like this:

"AuthDBContextConnection": "Server=localhost;Port=xxxx;Database=xxxx;User ID = xxx;Password=xxxx;Pooling=false;SslMode=none;convert zero datetime=True;Trusted_Connection=True;MultipleActiveResultSets=true"

I've tried googling it and checking stackoverflow for similar questions but can't find the solution. Every solution I've come across I need to change something in the web.config file. The problem is I don't even have a web.config file in my project.

Any ideas?

Thank you!

@camilo I’m not having problems writing the connection string. I’m having problems importing the database. The string should be correct since I’ve tried using it directly in my controller and it works. – leo Nov 3, 2021 at 16:52 @Camilo I’ve only used it in that string. Weird. I only tried it directly in the controller and then removed it. – leo Nov 3, 2021 at 16:55 Nevermind, that wasn't the problem. Microsoft.EntityFrameworkCore.SqlServer -> SQL Server -> you're using MySQL -> use the correct extension methods – Camilo Terevinto Nov 3, 2021 at 16:59

The error was similar while working with ASP.net core and Database. default database Provider with the ASP.net core is the SQL Server but, in case you are using a different provider, you will find this error.

looking like you are using MySQL DB in your project. update your code like below in your startup.cs file.

public void ConfigureServices(IServiceCollection services)
  services.AddDbContext<YourDbContextName>(options =>
                options.UseMySql(
                    Configuration.GetConnectionString("AuthDBContextConnection")));

Don't forget to install NuGet package MySql.Data.EntityFramework Hope it will resolve your issue.

UPDATE

Your Need these NuGet packages, please install these. it will resolve your issue.

using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using MySql.Data.EntityFrameworkCore

AGAIN UPDATE

Install these:-

Microsoft.EntityFrameworkCore (v5.0.0 – the latest stable version)
Microsoft.EntityFrameworkCore.Tools (v5.0.0 – the latest stable version)
Pomelo.EntityFrameworkCore.MySql (version 5.0.0-alpha.2)

Your connectionstring look like this:-

"ConnectionStrings": {  
    "DefaultConnection": "server=localhost; port=3306; database=test; user=root; password=Wxp@Mysql; Persist Security Info=False; Connect Timeout=300"  

And Update your startup.csfile:-

public void ConfigureServices(IServiceCollection services)  
            string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection");  
            services.AddDbContextPool<MyDBContext>(options => options.UseMySql(mySqlConnectionStr, ServerVersion.AutoDetect(mySqlConnectionStr)));  
            services.AddControllers();  

Now it will resolve your issue.

It's not working. I added the text and installed MySql.Data.EntityFramework. But "UseMySql" has an error. It says: "CS1061: 'DbContextOptionsBuilder' does not contain a definition for 'UseMySql' and no accessible extension method 'UseMySql' accepting a first argument of typ 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?) I have no idea what to write in "using". I've tried "using MySql.Data.EntityFrameWork" but same thing. – leo Nov 4, 2021 at 21:21 @Tarik Gen When I tried you solution I get this: "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: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)". Now the connection I'm trying to make is to my school's server via a tunnel. Could it by my school is blocking the connection? – leo Nov 4, 2021 at 21:45 I can't find the the nuget package "MySQL.Data.EntityFrameworkCore.Extensions". And I installed the MySql.DataEntityFrameworkCore. It is deprecated though. – leo Nov 5, 2021 at 11:54

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.