如何从Linux上的asp.net核心应用程序迁移表

时间:2019-07-01 14:30:35

标签: mysql linux asp.net-core

我制作了一个在Windows机器上完美运行的asp.net核心应用程序。我需要在Linux机器上部署此应用程序。将表迁移到Linux计算机上的MySQL服务器时,出现了我的问题。我拥有用于​​生产目的的Linux机器,并且需要MySQL服务器来处理用户。我已经创建了一个数据库,但是当我运行命令dotnet ef database update将表迁移到该数据库中时,看到以下错误:

enter image description here

用于连接数据库的连接字符串如下:

  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ElectransUsers;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

如果更改写入本地主机而不是localdb的连接字符串,则会看到以下错误: enter image description here

如果我删除mssqllocaldb,则会看到以下错误:

enter image description here 我在Startup.cs脚本中的Configure方法如下:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<IdentityUser, IdentityRole>()
            //.AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        services.AddSignalR();
    }

有人知道我必须更改连接字符串才能进行迁移吗? 谢谢。

我更改了我的连接字符串和代码:

       services.AddDbContext<DbContext>(options =>
           options.UseMySql(Configuration.GetConnectionString("MysqlConnection"),
               mySqlOptions =>
               {
                   mySqlOptions.ServerVersion(new Version(10, 1, 38), Pomelo.EntityFrameworkCore.MySql.Infrastructure.ServerType.MySql); // replace with your Server Version and Type
               }));

"MysqlConnection": "Server=localhost;Database=ElectransUsers;User=root;Password=my_password;"

但是我看到以下错误:

enter image description here

你知道我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

考虑使用MySQL连接提供程序。示例代码段在这里。

services.AddDbContext<DbContext>(options =>
   options.UseMySql(configuration.GetConnectionString("MysqlConnection"),
       mySqlOptions =>
       {
        mySqlOptions.ServerVersion(new Version(5, 1, 73), ServerType.MySql); // replace with your Server Version and Type
       })

我的连接字符串

 "MysqlConnection": "server=194.36.12.123;port=3306;database=database_name_here;uid=user_name_here;password=password_here"

我正在使用Pomelo.EntityFrameworkCore.MySql软件包

这里是documentation