自从容器开始流行起来后,虽然SqlServer也可以运行在容器中,但SqlServer只有开发版是免费的,查询速度太慢,不能用于生产。所以现在Entity Framewrok Core和mysql搭配成为不错的选择。
通过以下三步可以轻松实现Entity Framewrok Core搭配mysql。
在项目中添加Nuget包:
MySql.Data.EntityFrameworkCore
在Setup.cs文件中的ConfigService中配置DbContext,类似下面的代码:
services.AddDbContext<MyContext>(options => options.UseMySQL(Configuration.GetConnectionString("bd")));
在appsettings.json添加MySql连接配置
"ConnectionStrings": { "db": "server=localhost;database=dbname;user=username;password=******;" }