10 minutes easy to get to know entity Framework 7
EF7 is not updated on EF6, but is completely rewritten. It fully supports the. NET Core runtime, which means you can use EF on your Mac or Linux machine later on.
Https://github.com/nicholaspei/OrchardNoCMS I'm still investigating which ORM or micro ORM to use. Massive or dapper are not very suitable, because they all depend on System.Data, so cross-platform is a problem.
Now you are ready to use EF as an implementation, the bottom layer is a very high abstraction interface, and later if there is a suitable lightweight ORM, can be replaced.
Back to the point, EF7 than the larger change:
A. Support for Code-first or code only, no longer supports graphical interface generation.
B. Support relational database.
C. Cross-platform.
1. Start installing the EF7 package.
As described in the previous two articles, use KPM to install EF7 using SQL Server as a storage implementation (you can use Entityframework.inmemery to store data on the *inux platform).
After the installation is complete, you can see that the Project.json has changed.
To make it easier to use the EF,EF team we developed a command package that you can install to easily create code for data access. The following command to install Entityframework.commands
2. Use the command to generate the code.
This is a cool piece, compared to the previous version of. NET, which is a good learning other language highlights.
Here we need to edit Project.json to add a command to generate code from EF.
Like the K Web, we can create dbcontent files and corresponding SQL statements with K EF.
But let's start with K EF to see a magical thing:
You can see that K EF corresponds to three subcommands, context and migration, and help commands.
The context command is unable to create DbContext, so a dbcontext,mydbcontext.cs file needs to be created manually.
The next step is to execute the migration command, using migration to generate Code-first code.
Executes the K EF migration Add "initial" command.
Looking at the code file, you can see the new file as follows:
Finally we need to use the APPLY subcommand to generate the corresponding table in the database:
Execution: K EF Migration Apply
The corresponding table in the database is automatically created successfully:
The next step is to use EF for data access.
3.EF integrated into dependency injection.
The next thing you need to do is inject the EF-related content into the. NET5 default container to facilitate constructor injection.
Modify the Project.json as follows:
4. Modify the Controller and Add Views folder.
You can then modify the controller and add views.
HomeController file:
The corresponding view file:
\
Finally, run the application using the K Web:
Because there is no data in the table, count is 0. This completes the beginner's tutorial on using EF.
10 minutes easy to get to know entity Framework 7