ASP. NET Core 1.1 accesses Mysql and linux Through EF Core for debugging, efmysql

Source: Internet
Author: User

ASP. NET Core 1.1 accesses Mysql and linux Through EF Core for debugging, efmysql

1. Access Mysql

Getting started with ASP. NET Core MVC and Entity Framework Core using Visual Studio (1 of 10)

Write a simple test program based on the document.

 

In this case, bugs in earlier versions of VS2017 may not run. manually check for updates, tools-> extensions and updates-> updates

 

Enter

Install-Package MySql.Data.EntityFrameworkCore -Pre

 

Install the MySql-related provider (there is also a personal-developed provider, which will not be introduced in this article)

After installation, modify the ConfigureServices method in the startup file,

Change UseSqlServer to UseMySQL

1 public void ConfigureServices(IServiceCollection services)2 {3     services.AddDbContext<YourContent>(options =>4         options.UseMySQL(Configuration.GetConnectionString("MysqlConnection")));5 6     services.AddMvc();7 }

 

If the namespace cannot be recognized, manually add

using MySQL.Data.EntityFrameworkCore.Extensions;

 

7.0.7-m61 providers may need to enable or disable the connection before and after Mysql execution.

 

1 _context.Database.OpenConnection();2 await _context.SaveChangesAsync();3 _context.Database.CloseConnection();

 

If you want to execute stored procedures or SQL statements in three ways (1 and 2 are examples of SQL server, make appropriate modifications), you can refer to 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.Customers10         .FromSql(@"SELECT * FROM ""Customers"" WHERE ""City"" = @city", parameter)11         .ToArray();12 }

 

3. for traditional methods, here is an example of Mysql stored procedure call.

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. executeReaderAsy Nc (); 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 // 226 var result_num = _ context. database. executeSqlCommand ("sp_test_core (? P_id) ", parameter); 27 28 // 3 TEST_CORE is the DbSet29 test_cores = _ context. TEST_CORE.FromSql (@" call sp_test_core (? P_id) ", parameter). ToArray (); 30

 

DataTable is no longer available. It is not ruled out that it will be added back in a later version. Currently, DbDataReader is used to receive data.

Write the test page and you will see the results. MVC will not be detailed here.

 

 

Ii. linux debugging

 

The cross-platform web server provided by dotnet core is KestrelHttpServer, which copies the project file to the linux host. In the project directory, enter

dotnet restore

 

Enter

dotnet run

To run debugging.

 

If you want to remotely access the page in the LAN, after installing openssh and running,

Run the following command to customize the port. The ip address is the ip address allocated locally in the LAN. For details, refer to issue639.

ASPNETCORE_URLS="http://192.168.0.1:5000" dotnet run

 

The default port of dotnet core is localhost: 5000. You can also use the UseUrls custom port in the program.

 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");

 

For more information, see Introduction to Kestrel web server implementation in ASP. NET Core.

 

 

Iii. Release

To release the corresponding version, take ubuntu.16.04-x64 as an example. Add the corresponding RID in the csproject file. For details, refer to the. NET Core runtime identifier (RID) directory.

<PropertyGroup>    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>    <RuntimeIdentifier>ubuntu.16.04-x64</RuntimeIdentifier>  </PropertyGroup>

 

Console input

dotnet publish -r ubuntu.16.04-x64

You can publish to the ubuntu.16.04-x64 folder, refer to issue77

 

You can use reverse proxies such as Nginx to deploy core programs. for more information, see Set up a hosting environment for ASP. NET Core on Linux with Nginx, and deploy to it.

 

Reprinted Please retain the source http://www.cnblogs.com/kira-trash-can/p/6841403.html

 

Reference link:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.