mysql - ASP.NET核心: How to create a connection string on Macbook pro

标签 mysql asp.net-core database-connection visual-studio-code

我使用asp.net core创建了一个项目。我在 MacBook Pro 中安装了 Visual Studio Code,还有 MySql。

我的问题是如何在 mac 环境下使用 Visual Studio Code 将该项目连接到 MySql。

最佳答案

您应该能够找到存储应用程序环境变量的 appsettings.json 文件。在那里,您可以将数据库连接字符串设置为变量:

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

然后,在项目的 Startup.cs 文件中,您将找到一个名为“ConfigureServices”的方法。您可以在此处设置依赖项注入(inject),其中包括您的数据库上下文。在下面的示例中,我们通过访问“BloggingDatabase”环境变量并在将数据库上下文添加到项目服务时将其作为选项传递来指定要使用的连接字符串。

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<BloggingContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}

我建议您阅读以下 Microsoft 帮助文章的部分,其中包含有关 Asp.Net Core 项目中连接字符串的更多信息。

https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

关于mysql - ASP.NET核心: How to create a connection string on Macbook pro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263509/

相关文章:

php - 使用 mysql 在矩形或圆中查找点

php - 如何在 php 中有条件地链接方法?

MySQL - 任意行的 count()

mySQL 我是否需要多个索引来将具有外键的多个表连接到单个主表?

docker - 部署后编辑容器化应用程序的应用程序设置

docker - Unhealthy 和 Degraded .NET 健康检查状态有什么区别

c# - 如何在 Asp.Net Core 2.x 中将 root 重定向到 swagger?

java - 在 Java 中检测操作系统 sleep 和唤醒事件

.net - SYbase 使用 OLEDB 还是 ODBC

mysql - 当天开始时出现初始数据库连接错误