如何使用Dotnet EF脚手架工具将用户销售用于DBContext?

发布于 2025-01-29 23:09:15 字数 1241 浏览 2 评论 0 原文

在我的 appsettings.json 中,我存储连接字符串:

"ConnectionStrings": {
    "MyDb": "Server=localhost;Database=MYDB;User Id=sa"
}

我将此连接的密码存储为文档> documentation

dotnet user-secrets set "Password:Database:MyDb" "password-here"

在代码中,我在代码中使用连接建立了连接 sqlConnectionsTringBuilder

var conStrBuilder = new SqlConnectionStringBuilder(builder.Configuration.GetConnectionString("MyDb"));
conStrBuilder.Password = builder.Configuration["Password:Database:MyDb"];
builder.Services.AddSqlServer<MyDbContext>(conStrBuilder.ConnectionString);

在将密码移至用户销售之前,我只是将其添加到上面的连接字符串(即; password = password = password = password = password-here here )。这意味着我可以将以下内容与dotnet CLI一起使用:

dotnet ef dbcontext scaffold Name=ConnectionStrings:MyDb Microsoft.EntityFrameworkCore.SqlServer -o Models

当然,此返回登录失败了'sa'。

有没有办法在命令行上单独传递密码,因此我不必将密码放在源控件中,在命令行上纯净了吗?

In my appsettings.json I store the connection string:

"ConnectionStrings": {
    "MyDb": "Server=localhost;Database=MYDB;User Id=sa"
}

I store the password for this connection as a user-secret as per the documentation:

dotnet user-secrets set "Password:Database:MyDb" "password-here"

In code, I setup the connection using SqlConnectionStringBuilder:

var conStrBuilder = new SqlConnectionStringBuilder(builder.Configuration.GetConnectionString("MyDb"));
conStrBuilder.Password = builder.Configuration["Password:Database:MyDb"];
builder.Services.AddSqlServer<MyDbContext>(conStrBuilder.ConnectionString);

Before I moved the password to a user-secret, I was just adding it to the connection string above (i.e. ;Password=password-here). That meant I could then use the following with the dotnet CLI:

dotnet ef dbcontext scaffold Name=ConnectionStrings:MyDb Microsoft.EntityFrameworkCore.SqlServer -o Models

Now of course, this returns Login failed for use 'sa'..

Is there a way to separately pass the password on the command line, so I don't have to put the password in source control or have it plain on the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-02-05 23:09:15

@mycah的建议对我有用。我的连接存储在秘密管理器中,并具有以下结构:

"ConnectionStrings": {
    "SomeName": "<your_connection_string>"
}

DBContext设置为:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.UseSqlServer("name=SomeName");

@Mycah's suggestion worked for me. I had connection stored in secret manager with following structure:

"ConnectionStrings": {
    "SomeName": "<your_connection_string>"
}

The DbContext was setup as:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.UseSqlServer("name=SomeName");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文