前言:
最近在開始嘗試使用dotnet core做開發,dotnet core發布到1.1也越發成熟了,微軟提供的文檔也很詳細,跟着Getting started with ASP.NET Core MVC and Entity Framework Core using Visual Studio (1 of 10) 的步驟可以掌握一個基本的ASP.NET Core web application的創建方法。
而EF Core的文檔主要針對Sql Server,其他數據庫並沒有那么詳細,寫的過程中確實遇到一些問題,搜集各方資料,這里做一個匯總。
一、訪問Mysql
Getting started with ASP.NET Core MVC and Entity Framework Core using Visual Studio (1 of 10)
先根據文檔寫出一個簡單的測試程序,這里不再贅述
這里因為VS2017低版本的bug可能會不能運行,手動檢查更新,工具->擴展和更新->更新
然后在Package Manager Console輸入
Install-Package MySql.Data.EntityFrameworkCore -Pre
安裝MySql對應的provider(同時還有個人開發的provider,本文將不再介紹)
安裝好后,修改startup文件中ConfigureServices方法,
將UseSqlServer改為UseMySQL
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.AddDbContext<YourContent>(options => 4 options.UseMySQL(Configuration.GetConnectionString("MysqlConnection"))); 5 6 services.AddMvc(); 7 }
此時若不能識別命名空間,需要手動添加
using MySQL.Data.EntityFrameworkCore.Extensions;
7.0.7-m61版本的provider訪問Mysql可能需要在執行前后打開和關閉連接
1 _context.Database.OpenConnection(); 2 await _context.SaveChangesAsync(); 3 _context.Database.CloseConnection();
如果要執行存儲過程或者sql有三種方法(1和2是sql server的例子,做適當修改即可),可以參考issue3115
1
1 using (var context = new NorthwindContext()) 2 { 3 var parameter = new SqlParameter 4 { 5 ParameterName = "@CustomerID", 6 Value = "ALFKI" 7 } 8 9 context.Database.ExecuteSqlCommand("[dbo].[CustOrderHist] @CustomerID", parameter) 10 }
2
1 using (var context = new NorthwindContext()) 2 { 3 var parameter = new SqlParameter 4 { 5 ParameterName = "@City", 6 Value = "London" 7 } 8 9 var customers = context.Customers 10 .FromSql(@"SELECT * FROM ""Customers"" WHERE ""City"" = @city", parameter) 11 .ToArray(); 12 }
3以及傳統方法,這里補充上Mysql存儲過程調用的例子
1 var test_cores = new Test_Core[] { }; 2 var test_core = new Test_Core(); 3 4 var parameter = new MySqlParameter("?p_id", MySqlDbType.Int16); 5 parameter.Value = 1; 6 parameter.Direction = ParameterDirection.Input; 7 //1 8 using (var cmd = _context.Database.GetDbConnection().CreateCommand()) 9 { 10 _context.Database.OpenConnection(); 11 12 cmd.CommandType = CommandType.StoredProcedure; 13 cmd.CommandText = "sp_test_core"; 14 cmd.Parameters.Add(parameter); 15 DbDataReader result; 16 result = await cmd.ExecuteReaderAsync(); 17 while (result.Read()) 18 { 19 test_core.Id = int.Parse(result[0].ToString()); 20 test_core.key = result[1].ToString(); 21 test_core.value = result[2].ToString(); 22 } 23 _context.Database.CloseConnection(); 24 } 25 //2 26 var result_num = _context.Database.ExecuteSqlCommand("sp_test_core(?p_id)", parameter); 27 28 //3 TEST_CORE是content中定義的model的DbSet 29 test_cores = _context.TEST_CORE.FromSql(@"call sp_test_core(?p_id)", parameter).ToArray(); 30
DataTable目前已經沒有了,不排除會在之后的版本加回來的可能性,現在接收數據使用DbDataReader
編寫測試頁面即可看到結果,MVC相關這里不再贅述。
二、linux調試
dotnet core提供的跨平台的web server為KestrelHttpServer,將項目文件完整拷貝到linux機上,在項目目錄先輸入
dotnet restore
再輸入
dotnet run
即可運行調試。
如果想要在局域網中遠程訪問頁面,在安裝openssh並運行之后,
通過以下命令運行,即可自定義端口,ip為局域網中本地分配的ip,參考issue639
ASPNETCORE_URLS="http://192.168.0.1:5000" dotnet run
dotnet core默認端口是localhost:5000,也可以在程序中使用UseUrls自定義端口
1 var builder = new WebHostBuilder() 2 .UseContentRoot(Directory.GetCurrentDirectory()) 3 .UseConfiguration(config) 4 .UseStartup<Startup>() 5 .UseKestrel(options => 6 { 7 if (config["threadCount"] != null) 8 { 9 options.ThreadCount = int.Parse(config["threadCount"]); 10 } 11 }) 12 .UseUrls("http://localhost:5000");
更多相關可參考Introduction to Kestrel web server implementation in ASP.NET Core
三、關於發布
如果想發布對應版本,這里以ubuntu.16.04-x64為例,在csproject文件中添加對應的RID,參考.NET Core 運行時標識符 (RID) 目錄
<PropertyGroup> <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback> <RuntimeIdentifier>ubuntu.16.04-x64</RuntimeIdentifier> </PropertyGroup>
控制台輸入
dotnet publish -r ubuntu.16.04-x64
即可publish到 ubuntu.16.04-x64文件夾,參考issue77
可以通過Nginx等反向代理來部署core程序,參考Set up a hosting environment for ASP.NET Core on Linux with Nginx, and deploy to it
轉載請保留出處http://www.cnblogs.com/kira-trash-can/p/6841403.html
參考鏈接:
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro
https://docs.microsoft.com/en-us/ef/core/providers/
https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/
https://github.com/aspnet/EntityFramework/issues/3115
https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction
https://github.com/aspnet/KestrelHttpServer/issues/639
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel