900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > ef mysql 配置字符串_连接字符串-EF Core | Microsoft Docs

ef mysql 配置字符串_连接字符串-EF Core | Microsoft Docs

时间:2024-06-06 14:26:31

相关推荐

ef mysql 配置字符串_连接字符串-EF Core | Microsoft Docs

连接字符串Connection Strings

10/27/

本文内容

大多数数据库提供程序都需要某种形式的连接字符串才能连接到数据库。Most database providers require some form of connection string to connect to the database. 有时,此连接字符串包含需要保护的敏感信息。Sometimes this connection string contains sensitive information that needs to be protected. 在开发、测试和生产等环境之间移动应用程序时,可能还需要更改连接字符串。You may also need to change the connection string as you move your application between environments, such as development, testing, and production.

Core

在 Core 配置系统非常灵活,并且可以将连接字符串存储在 appsettings.json 、环境变量、用户密钥存储或其他配置源中。In Core the configuration system is very flexible, and the connection string could be stored in appsettings.json, an environment variable, the user secret store, or another configuration source. 有关更多详细信息,请参阅 Core 文档 的 "配置" 部分。

例如,你可以使用 机密管理器工具 存储数据库密码,然后在基架中,使用只包含的连接字符串 Name= 。For instance, you can use the Secret Manager tool to store your database password and then, in scaffolding, use a connection string that simply consists of Name=.

dotnet user-secrets set ConnectionStrings:YourDatabaseAlias "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=YourDatabase"

dotnet ef dbcontext scaffold Name=ConnectionStrings:YourDatabaseAlias Microsoft.EntityFrameworkCore.SqlServer

下面的示例显示了中存储的连接字符串 appsettings.json 。Or the following example shows the connection string stored in appsettings.json.

{

"ConnectionStrings": {

"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"

},

}

然后,上下文通常在中配置为在 Startup.cs 从配置中读取的连接字符串中。Then the context is typically configured in Startup.cs with the connection string being read from configuration. 请注意, GetConnectionString() 方法查找其键为的配置值 ConnectionStrings: 。Note the GetConnectionString() method looks for a configuration value whose key is ConnectionStrings:. You need to import the Microsoft.Extensions.Configuration namespace to use this extension method.

public void ConfigureServices(IServiceCollection services)

{

services.AddDbContext(options =>

options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));

}

WinForms & WPF 应用程序WinForms & WPF Applications

WinForms、WPF 和 4 应用程序都有一个已尝试并经过测试的连接字符串模式。WinForms, WPF, and 4 applications have a tried and tested connection string pattern. 如果使用 ) ,则应将连接字符串添加到应用程序的 App.config 文件中 ( # A1。The connection string should be added to your application's App.config file (Web.config if you are using ). 如果你的连接字符串包含敏感信息(例如用户名和密码),则可以使用 受保护的配置来保护配置文件的内容。If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using Protected Configuration.

connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" />

提示

providerName由于数据库提供程序是通过代码配置的,因此在 App.config 中存储的 EF Core 连接字符串上不需要此设置。The providerName setting is not required on EF Core connection strings stored in App.config because the database provider is configured via code.

然后,你可以 ConfigurationManager 在上下文的方法中使用 API 来读取连接字符串 OnConfiguring 。You can then read the connection string using the ConfigurationManager API in your context's OnConfiguring method. 你可能还需要添加对 System.Configuration 框架程序集的引用才能使用此 API。You may need to add a reference to the System.Configuration framework assembly to be able to use this API.

public class BloggingContext : DbContext

{

public DbSet Blogs { get; set; }

public DbSet Posts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);

}

}

通用 Windows 平台 (UWP)Universal Windows Platform (UWP)

UWP 应用程序中的连接字符串通常是一个 SQLite 连接,只需指定本地文件名。Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. 它们通常不包含敏感信息,并且在部署应用程序时无需更改。They typically do not contain sensitive information, and do not need to be changed as an application is deployed. 因此,这些连接字符串通常可以保留在代码中,如下所示。As such, these connection strings are usually fine to be left in code, as shown below. 如果希望将它们移出代码,则 UWP 支持设置概念,有关详细信息,请参阅 uwp 文档的 "应用设置" 部分 。If you wish to move them out of code then UWP supports the concept of settings, see the App Settings section of the UWP documentation for details.

public class BloggingContext : DbContext

{

public DbSet Blogs { get; set; }

public DbSet Posts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

optionsBuilder.UseSqlite("Data Source=blogging.db");

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。