我使用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/