简体   繁体   中英

I can't connect to a local SQL Server instance via Entity Framework using Windows authentication

I can't connect to my local SQL Server Express instance via Entity Framework. When I try to run the update-database command, I get this error message.

Login failed for user ''. Reason: An attempt to login using SQL authentication failed.
Server is configured for Integrated authentication only.

Error: 18456, Severity: 14, State: 58.

From what I understand, Visual Studio is attempting to log in to SQL Server via a user account even though I've requested that Windows authentication is used in the connection string.

I can still access the server via SSMS.

What I've tried. None of which helped

  • Different variations of the connection string
  • Opened port 1433
  • Created a test UDL file to test the connection.
  • A clean reinstall of SQL Server Express (not sure I managed to clean up all the files)
  • Checked the SQL Server browser is running
  • Checked server instance is running
  • Enabled TCP/IP & named pipes
  • Tried connecting via tcp which works
  • Added Integrated Security=SSPI to connection string
  • Changed server to accept Windows authentication and SQL Server authentication.
  • Restarting PC

This is a new laptop on windows 11, I don't know if that is causing any issues as I've never had an issue with this process on Windows 10.

I seem to have two instances; .\SQLEXPRESS & (localdb)\\MSSQLLocalDB I'm not sure if this is causing some conflict or if this is the intended behaviour.

Here is the connection string, I pulled this from the server explorer within Visual Studio so I am pretty sure this is correct. On top of that, I have tried numerous variations of this to attempt to fix the issue

"DefaultConnection": "Data Source=LAPTOP-51LB4QTQ\\SQLEXPRESS;Initial Catalog=MicroBlog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

This is where I get the connection string in my Program.cs file

builder.Services.AddDbContext<BlogContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();

At this point I'm completely lost, I've read a lot of articles but have not come across any fixes. I'm not a dba just a programmer so have limited knowledge of this side of SQL Server.

You may try like below:

In Web.config file:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=SUBRATATALUKDER;Initial Catalog=MyDB;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

In appsettings.json file:

"ConnectionStrings": {
    "DefaultConnection": "Server=SUBRATATALUKDER;Database=MyDB;Trusted_Connection=True;TrustServerCertificate=True;",
}

Note: Server Name = SUBRATATALUKDER

Database Name = MyDB

100% tested.

SQL Server 连接字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM