使用Entity Framewrok Core连接Mysql

Posted jiutong9u

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Entity Framewrok Core连接Mysql相关的知识,希望对你有一定的参考价值。

自从容器开始流行起来后,虽然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=******;"
    }

以上是关于使用Entity Framewrok Core连接Mysql的主要内容,如果未能解决你的问题,请参考以下文章