sql-server.net-corescaffolding

How to use Windows authentication with the Scaffold-DbContext command?


When I run the following command in the package manager console in Visual Studio 2022:

Scaffold-DbContext 'Server=computename/user;Database=pub;Integrated Security=true; TrustServerCertificate=true' Microsoft.EntityFrameworkCore.SqlServer

I'm getting this error:

Cannot open database "pub" requested by the login. The login failed.
Login failed for user 'computername\user'.

This is a local SQL Server database I am trying to scaffold using Windows authentication. I added the TrustServerCertificate attribute because earlier I was getting an error about certificates. The user is my computer name followed by my windows user name. I am trying to follow the scaffold tutorial here:

https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=vs#command-line-options

I don't plan on using SQL Server authentication for a while. I would like to see some basic example of how to run this command using windows authentication, or some settings I can tweak to make this command compatible with using Windows authentication with the local instance I have running on my own machine.


Solution

  • It seems I made a typo in the connection string:

    scaffold-DbContext 'Server=Servername; Database=pubs;Integrated Security=true;    TustServerCertificate=true' Microsoft.EntityFrameworkCore.SqlServer   
    

    The Northwind sample db I was trying to scaffold was named 'pubs' not pub. For anyone else trying to follow this tutorial, locally, you will need to have the TustServerCertificate attribute set to true for development purposes. The server is often your computer name unless it is named otherwise and If you're using windows authentication you'll need to have the 'Intergrated Security' attribute set to true.