dotnet / dotnet/EntityFramework.Docs

Example for lazy initialization of connectionstring does not work when using password in connectionstring

Open
#4,101 2 comments 0 reactions 0 assignees View on GitHub
area-interceptors
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

Wasted some time debugging issue where sql login failed for user when using this approach for setting connection string.

When using connection string with a password it will be correct the first time the interceptor is invoked, but the next time we open connection the connection.ConnectionString is not empty - but the password is removed due to Persist Security Info is default false.
```
if (string.IsNullOrEmpty(connection.ConnectionString))
{
connection.ConnectionString = (await _connectionStringFactory.GetConnectionStringAsync(cancellationToken));
}
```
[Lazy initialization of a connection string](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#lazy-initialization-of-a-connection-string)

For this to work with password the connection string must be set each time and probably use some caching or change the default...

Maybe the docs should have a note about this?

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 9c8aad46-30fc-c5ef-8be2-5b94a0ded8eb
* Version Independent ID: 9c8aad46-30fc-c5ef-8be2-5b94a0ded8eb
* Content: [What's New in EF Core 7.0](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew)
* Content Source: [entity-framework/core/what-is-new/ef-core-7.0/whatsnew.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/what-is-new/ef-core-7.0/whatsnew.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.