【问题标题】:I can't connect to a local SQL Server instance via Entity Framework using Windows authenticationI can\'t connect to a local SQL Server instance via Entity Framework using Windows authentication
【发布时间】:2022-12-02 00:50:08
【问题描述】:

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.

【问题讨论】:

  • I would delete this part from the connection string, and check if the error message is getting clearer: ;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
  • or, pick an appropriate connection string from: connectionstrings.com/sql-server
  • @Luuk I've tried those variations on connectionstrings.com and without the extra fluff on the end of my current one. I only included here it just in case it was useful. Unfortunately no variation of connection string has helped
  • Find the instance's ERRORLOG file, find some examples of the 18456 event within it and take note of the SQL Login username that it's trying to login with. Then search your entire codebase for that username. Entity Framework's migration code has a tendency to hard-code connection string properties in the C# itself instead of using the default connection string from the .json/.config files.

标签: c# sql-server asp.net-core entity-framework-core


【解决方案1】:

You may try like below:

InWeb.configfile:

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

Inappsettings.jsonfile:

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

Note:Server Name=SUBRATATALUKDER

Database Name=MyDB

100%tested.

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 2022-12-27
    • 2022-12-27
    • 2022-12-27
    • 2022-12-02
    • 2022-11-20
    • 2022-12-27
    • 2023-03-20
    • 2022-12-02
    相关资源
    最近更新 更多