NET core access, manage MySQL through Ef core
This address: http://www.cnblogs.com/likeli/p/5910524.html
Environment
dotnet Core Version:1.0.0-preview2-003131
This article is divided into a window environment and a MAC Os x environment.
Related Resources Download
Visual Studio code:https://code.visualstudio.com
DotNet core:https://dotnet.github.io/
Mysql.data.entityframeworkcore:http://www.nuget.org/packages/mysql.data.entityframeworkcore/7.0.5-ir21
Entity Framework Core (pomelo): Https://docs.efproject.net/en/latest/providers/pomelo/index.html#supported-database-engines
Entity Framework Core (official): https://docs.efproject.net/en/latest/providers/mysql/
MySQL Database
Installation configuration is not introduced, not the focus of this article, in this article, the Mac under the use of MySQL is brew
directly installed through the package manager.
Log in to the database to check the contents of the current database
OS x Environment
Install the Dotnet Core SDK package and the visual Studio Code installation package, the above resources have, if you do not know how to configure, please see my other two articles, which is introduced: http://www.cnblogs.com/likeli/p/ 5883551.html, http://www.cnblogs.com/likeli/p/5910475.html
Create a Web project
Execute the command under the prepared project directory and create it:
new -t web
Visual Studio Code
after you have installed the C # plug-in, you can color, prompt , and so on.
Import MySQL driver package
Open the browser, go NuGet
shopping, search the MySQL official out of the driver packMySql.Data.EntityFrameworkCore
You can see the library name above MySql.Data.EntityFrameworkCore
and the version number7.0.5-IR21
Open the project project.json
, include the name of the MySQL driver package in the file, and the version number.
Then type the command in the terminal to reply to all the dependent packages via NuGet:
restore
adding entities and contexts
Create a new. cs file under the Models folder of the project, join User.cs
, Blog.cs
.
The code is as follows:
User.cs
namespace WebApplication.Models{ public class User { public int UserId { set; get; } public string Name{set;get;} }}
Blog.cs
Namespacewebapplication.models{public class blog{ public int id{set; get;} public string title{set; get;} public string content{set; get;} public int userid{set; get;} public Virtual User user{set; get;} }}
Under the Data folder, add the context:
DataContext.cs
Using Microsoft.entityframeworkcore;Using MySQL.Data.EntityFrameworkCore.Extensions;Using Webapplication.models;namespace webapplication.data{ Public class datacontext: DbContext{ public dbset<user> users{set; get;} public dbset<blog> blogs{set; get;} protected override void onconfiguring ( Dbcontextoptionsbuilder Optionsbuilder) = Optionsbuilder.usemysql (@ "Server=localhost;database=ef;uid=root"; Pwd=root "); }}
Developers with EF experience should be able to understand the process very easily, in fact, it is necessary to explain the development in non-Visual Studio, can not directly use the NuGet command, directly in the project.json
add the need to reference the package, and then execute the dotnet restore
restore package.
Then write the calling code inside the home controller:
using(var context = new DataContext()){ context.Database.EnsureCreated(); var user = new User {Name="愤怒的TryCatch"}; context.Add(user); context.SaveChanges();}
Compiling tests
Type the command in the terminal dotnet build
to compile and then ' dotnet run '.
Look at the changes in the database:
You can see the MySQL data with a library called Ef
The table structure has also been established, and new data has been added to the user table.
Now add the blog data, here to show the foreign key relationship.
There are also foreign key relationships found.
In summary , it seems that the use of EF Core is not very different from that of EF on Windows, but it is only an earlier version and the version should be quickly iterated for production, so think twice.
This is a demo of EF core on Mac, I've tested it on windows, and I've written it the same way. You can do it by analogy, please.
NET core access, manage MySQL through Ef core