43

I am trying to build an API using Visual Studio 2017 and .NET Core 2 with Entity Framework Core. I am following the directions from This Link. I am on the section titled: Reverse engineer your model

The command they give to run is as follows:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

My database is an Azure SQL database so I am trying to run this command using the connection string provided from the Azure portal:

Scaffold-DbContext "Server=tcp:dbname.database.windows.net,1433;Initial Catalog=DBNAME_DB;Persist Security Info=False;User ID=username;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

I am getting returned a

System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'username'.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:ba6ca255-04c8-46c2-9d4f-77204728e9f2
Error Number:18456,State:1,Class:14
Login failed for user 'username'.

From this same machine and username and password I am able to login to the database using SSMS. Does anyone have any suggestions why I am unable to run this command from Visual Studio?

1
  • 3
    General tip if you ever get this kind of login error and you're sure the password is correct - first change the username in the string to nonsense like bigdevjamesXXX. If you still get login failed for bigdevjames it means you're looking in the wrong place in the wrong or old config file. In my case (for the second time now) it was a pesky $ in the password. Mar 17, 2018 at 18:18

5 Answers 5

142

I figured out my problem. My password contained the character $ and this needs to be escaped out by using ` before the $.

7
  • 1
    In my case ` didn't work, I'm using .Net Core 2.1, so my solution was update the password deleting $ symbols. > ALTER LOGIN yoursqllogin WITH PASSWORD = 'newpassword' OLD_PASSWORD = 'oldpassword' Dec 18, 2018 at 0:56
  • 2
    @davidrl1000 use a backslash instead of backquote to escape a $.
    – billb
    Feb 27, 2019 at 21:27
  • 2
    Backquote worked for me. Of course! Why not a backquote? (My target database was MS SQL Server on Azure.)
    – catfood
    May 10, 2019 at 4:35
  • 8
    If the Package Manager Console is using PowerShell you could also use single quotes instead of double so you don't have to escape out characters Scaffold-DbContext 'Server=(localdb)\m ...' Aug 21, 2019 at 15:50
  • 8
    Changing " with ' solved my problem. Scaffold-DbContext 'Server=tcp:dbname.database.windows.net,1433;Initial Catalog=DBNAME_DB;Persist Security Info=False;User ID=username;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
    – syalcin
    May 25, 2020 at 23:45
18

Change double quotes " for single quotes ' It is not necessary to add a escape character individually, it works for the rest of the parameters too.

Scaffold-DbContext 'Server=tcp:dbname.database.windows.net,1433;Initial Catalog=DBNAME_DB;Persist Security Info=False;User ID=username;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
3
  • 5
    Underrated answer Jan 24, 2022 at 15:50
  • Hello @JsonStatham is another way to solve the problem with special characters...
    – mainmind83
    Jan 24, 2022 at 18:52
  • Seems the behavior changed in year 2021 or 2022 for dotnet6/efcore, I can login until it suddenly doesn't work after a recent upgrade from dotnet5 to dotnet6.
    – zwcloud
    Nov 22, 2022 at 5:32
0

Did you tried to update your web.config file for connection string. This might be a reason which doesn't allow you to login to azure.

1
  • 1
    I do not have a web.config file. I haven't ever deployed this, and I am still building it. I'm not sure yet that I will be hosting this behind IIS. Regardless, I am trying to reverse engineer the database into Entities in my code. If anything, I would expect the string to be in the appsettings.json file, but I don't have one of those yet either. Can you show me what you think should be there? Nov 20, 2017 at 19:42
0

Please make sure the user login exists on the target SQL Azure database and not only on the Azure SQL Database server.

-- On master database CREATE LOGIN MaryLogin WITH PASSWORD = '';

-- On the user database

CREATE USER MaryUser FROM LOGIN MaryLogin;

Create a firewall rule on Azure portal to allow access to the Azure SQL Database as explained here. Make sure port TCP 1433 is open from your computer.

-1

For me the below command worked when executed in Package Manager console:PM>

Scaffold-DbContext "Server=.;Database=Employee;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 27, 2022 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.