Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document when PersistSecurityInfo=True is needed #2926

Open
umbarov opened this issue Dec 4, 2020 · 4 comments
Open

Document when PersistSecurityInfo=True is needed #2926

umbarov opened this issue Dec 4, 2020 · 4 comments

Comments

@umbarov
Copy link

umbarov commented Dec 4, 2020

File a bug

When I pass to UseSqlServer() connection string, it connects to the docker db and local db. But when I pass to UseSqlServer() new Microsoft.Data.SqlClient.SqlConnection(connString), it fails to connect to the docker db but connects to the local db. Simple repo https://github.com/umbarov/dotnet-sqlserver-gh-actions with Github Actions.

Connects to the docker db and local sql server:

var context = new AppDbContext(new DbContextOptionsBuilder<AppDbContext>().UseSqlServer(connString).Options);

Fails to connect to the docker db, but connects to the local sql server:

var context = new AppDbContext(new DbContextOptionsBuilder<AppDbContext>().UseSqlServer(new SqlConnection(connString)).Options);

Include stack traces

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'sa'.
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose
)

Include provider and version information

EF Core version: 5.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10 Pro , Version: 20H2, OS build: 19042.630

@smitpatel
Copy link
Contributor

You need PersistSecurityInfo=True in you connection string when you pass us SqlConnection because by default it will strip out the password. So the connection EF Core has does not have password information in it to connect correctly.
This does not happen for localdb because you are using Trusted_Connection which effectively uses your user login.

@umbarov
Copy link
Author

umbarov commented Dec 4, 2020

@smitpatel thanks. With PersistSecurityInfo=True it works.

@umbarov umbarov closed this as completed Dec 4, 2020
@ajcvickers ajcvickers reopened this Dec 4, 2020
@ajcvickers ajcvickers changed the title Fails to connect to docker sqlserver db via EF Core DbContext and SqlConnection Document when PersistSecurityInfo=True is needed Dec 4, 2020
@ajcvickers ajcvickers transferred this issue from dotnet/efcore Dec 4, 2020
@ajcvickers ajcvickers added this to the Backlog milestone Dec 4, 2020
@ajcvickers
Copy link
Member

Moved to docs to document when this is needed. (Basically when the application passes a SqlConnection to us, but we then need to connect to the master database. This means we have to clone the connection, and we lose the password.)

@umbarov
Copy link
Author

umbarov commented Dec 4, 2020

@ajcvickers I saw examples on this page https://docs.microsoft.com/en-us/ef/core/testing/sharing-databases#the-fixture when was writing unit tests. Someone else might step on the rake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants