You can easily get to know Entity Framework 7 and entityframework in 10 minutes.

Source: Internet
Author: User

You can easily get to know Entity Framework 7 and entityframework in 10 minutes.

EF7 is not updated on EF6, but completely rewritten. It fully supports. NET Core runtime, which means you can use EF on Mac or Linux machines in the future. Https://github.com/nicholaspei/OrchardNoCMS I am still investigating which ORM or Micro ORM to use. Massive or Dapper is not suitable because they all depend on System. Data, so cross-platform is a problem.

At present, we are going to use EF as an implementation, and the underlying layer is a very high abstract interface. If there is a suitable lightweight ORM in the future, we can replace it.

Back to the topic, EF7 has changed a lot:

A. Only Code-First or Code is supported. Gui generation is not supported.

B. Supports relational databases.

C. Cross-platform.

 

1. Start to install the EF7 package.

As described in the previous two articles, use kpm to install ef7 and use SQL Server as the storage implementation (you can use EntityFramework. InMemery to store data on * inux platform ).

After the installation is complete, you can see that project. json has changed.

To make it easier to use EF, the EF team developed a command package for us. You can install this command package to easily create data access code. The following command installs EntityFramework. Commands

 

 

2. Use commands to generate code.

This is cool. Compared with the previous version of. NET, it is a good learning highlight for other languages.

Next we need to edit project. json to add a command to generate code through ef.

Similar to k web, we can use k ef to create a DbContent file and corresponding SQL statements.

But let's look at a magic thing through k ef:

We can see that k ef has three sub-commands: context, migration, and help commands.

The context command cannot create DbContext. Therefore, you must manually create a DbContext and MyDbContext. cs file.

Next, execute the migration command and use migration to generate code-first.

Run the k ef migration add "initial" command.

View the code file and you will see the generated file as follows:

 

Finally, we need to use the apply sub-command to generate the corresponding table in the database:

 

Run: k ef migration apply

 

The corresponding table in the database is automatically created:

Next, you can use EF for data access.

3. EF is integrated into Dependency Injection ..

What needs to be done below is to inject EF-related content into the default. NET5 container to facilitate constructor injection.

Modify project. json as follows:

 

4. Modify the Controller and add the Views folder.

Then you can modify the Controller and add Views.

HomeController file:

 

Corresponding View File:

 

Finally, run the application on K web:

 

Because there is no data in the table, count is 0. Therefore, the basic tutorial of EF is completed.

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.