簡體   English   中英

EF Core:如何為Linux配置mySql,為Windows配置localDB?

[英]EF Core: how do I configure mySql for Linux, and localDB for Windows?

我有一個使用LocalDB的簡單ASP.Net Core / Entity Framework Core項目。 它可以在Windows上編譯並正常運行。

我想在Windows和Linux上構建並運行相同的項目。 但是Linux不支持LocalDB。 因此,我需要將項目配置為改為使用mySql-但僅適用於Linux。

問:如何配置項目,以便可以在Windows上使用LocalDB,而在Linux上使用mySql?

到目前為止,這是我嘗試過的:

  1. 創建一個空的mySql數據庫並授予對mySql用戶的訪問權限。

  2. appsettings.json創建了一個mySql連接字符串:

     { "ConnectionStrings": { "DefaultConnection": "Server=(LocalDb)\\\\MSSQLLocalDB;Database=ManageCarDB;Trusted_Connection=True;MultipleActiveResultSets=true", "mySqlConnection": "Server=localhost;port=3306;database=ManageCarDb;uid=dotnetuser;password=dotnetuser" },... <= I've defined two different connection strings: one for LocalDB, one for MySql 
  3. 更新了Startup.cs

     public void ConfigureServices(IServiceCollection services) { string env = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); string connectionString; if (!string.IsNullOrEmpty(env) && env.Equals("Linux")) { connectionString = Configuration.GetConnectionString("mySqlConnection"); services.AddDbContext<ApplicationDbContext>(options => options.UseMySQL(connectionString)); } else { connectionString = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString)); } 

    <=啟動將有條件地調用MySql連接字符串/ MySQL數據提供程序或default / LocalDB

  4. 在Linux上:

    1. 刪除所有二進制文件
    2. dotnet restore
    3. dotnet ef migrations add newMigration -c ApplicationDbContext -v

    <=一切正常

  5. 試圖更新數據庫:

    dotnet ef database update

    <= 錯誤:表'ManageCarDb .__ EFMigrationsHistory'不存在

問:鑒於我想要針對兩個EF環境的一個項目,我是否采取了正確的步驟?

還是應該采用其他方法?

您應該使用`Pomelo.EntityFrameworkCore.MySql而不是Oracle的MySql庫。

我使用Pomelo.EntityFrameworkCore.MySql ,它在我的項目中運行良好。

Oracle的MySql庫不像我嘗試的那樣支持遷移。 這個圖書館面臨幾個問題

注意 :我正在Oracle網站上找到一個討論此問題的鏈接

錯誤:該方法或操作未實現。 在搭建MYSQL數據庫時

https://bugs.mysql.com/bug.php?id=90368

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM