ASP.Net核心:如何在Macbook pro上创建连接字符串

ASP.Net Core: How to create a connection string on Macbook pro

我使用 asp.net 内核创建了一个项目。我在我的 MacBook Pro 中安装了 visual studio 代码,我也有 MySql。

我的问题是如何在 mac 环境中使用 visual studio 代码将该项目连接到 MySql。

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

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

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

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

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

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

其实MySql.Data的新版本(8.0.0)需要这个连接String架构。

 "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;port=3306;Database=database1;user id=root;SslMode=none;"
  }