Efcore 6.0 entry. This is enough



As a coder who has been working in the dotnet industry, many ORM frameworks have been involved in large and small projects in recent years, such as efcore, dapper, NHibernate, sqlsugar, etc. These ORM have their own advantages and disadvantages. People have different opinions on their choices. In fact, it’s good to meet the needs of their own projects. There is no best one, but the most suitable one.
As far as my own situation is concerned, I still use EF core more. In retrospect, I can be regarded as a loyal fan of efcore, hahaha!
Recently, when flipping the EF core official website, I saw the latest efcore 6 In priview4 of 0, the efcore development team will improve the performance of efcore. Efcore 6 will be 70% faster than efcore 5, improve the query execution speed by 31%, and reduce the heap allocation by 43%. In the techempower fortunes benchmark, the gap between dapper and EF core narrowed from 55% to just under 5%. At this point, we can look forward to ef core7.
Students interested in this aspect can go to the official website to learn:
EF Core6. What’s new in 0
Announcing Entity Framework core 6.0 preview version 4: performance version
See here, those students who always want to use EF core and worry that the performance of EF core can not meet the project requirements can now use it boldly.

In this tutorial, I will teach you to use EF core step by step from the perspective of Xiaobai, and I will follow the standards of project development. Students can apply it directly to practical projects.
Of course, if the industry leaders come to visit, let’s take a step and don’t teach others.
If you are a student who doesn’t know about EF core before, you can also stop here for a while, drink tea and eat melon seeds. Listen to me.

Technical preparation

  • Visual Studio 2022
  • .Net Core 6
  • EF Core 6
  • SQL Server

Construction project

Create project

Open visual studio 2022 and select ASP Net core empty project based on Net6 create

Note: Net 6 has set startup And file cs.program CS unified into a single program CS file
I want to know For more changes in net6, please move to the portal of the official website.Net 6
image 2.png

Install Entity Framework core

Let’s install several nuget packages first (which will be used later):
Microsoft. EntityFrameworkCore. Sqlserver (for EF core SQL Server Provider)
Microsoft. EntityFrameworkCore. Design (for EF core. Net core cli tools)
Microsoft. EntityFrameworkCore. Tools (package manager console tools for EF core)

Install using the visual studio nuget package manager console

Entry: Tools > nuget package manager > package manager console

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.Design

image 3.png

Install using nuget package manager

image 5.png

Create entity class

Blog and post establish a one to many relationship
image 6.png
image 7.png

Create context class

Note: database context class is the main class to coordinate EF core functions for a given data model. Context derived from Microsoft EntityFrameworkCore. DbContext。 Context specifies which entities are included in the data model.

Create a new bloggingcontext CS, and then inherit dbcontext
image 8.png

Then we need to pass the configuration of our bloggingdbcontext to dbcontext

The applicationdbcontext class must expose dbcontextoptionsPublic constructor for parameter. This is the way to pass the context configuration of adddbcontext to dbcontext

After this configuration, bloggingdbcontext can be injected into ASP. Net through the constructor Net core controller or other services:
For example:
image 10.png

We need to add our models to the context through the dbset attribute in the context and expose them

Because an entity set contains multiple entities, many developers prefer to use the plural dbset attribute name

  • One is created for each entity setDbSetProperties. In EF core terminology:
    • Entity sets usually correspond to database tables.
    • The entity corresponds to the row in the table.

image 11.png

We override the onmodelcreating method to further configure the entity we just added to the context
image 12.png

There are several configurations (recommended method 2). Fluent API configuration or data annotation can be used here (not recommended)
See the official website for detailsEF core entity type configuration
(1) It can be directly stacked in onmodelcreating (not recommended):

(2) Through ientitytypeconfigurationConfiguration (recommended)
We create the corresponding entitytypeconfiguration configuration file for the corresponding entity
image 14.png
image 15.png
image 16.png
For more information about the configuration of fluent API, please go to the official websiteCreate model

Inject dbcontext

image 17.png
appsettings. Development. JSON self configuring database connection string

Use code first to generate database based on entity

Open the package management console

Step 1: generate migration file:
Here are a few things to pay attention to
(1) The solution can be compiled
(2) Set target project as startup item
(3) Select the target project in the default project column in the package management console

Add migration init just press enter

The second step is to execute the generated migration file into the DB

Enter update database and press enter directly

image 20.png
If done appears, the execution is completed
image 21.png
We can check the database
image 22.png
The corresponding table structure we configured was successfully generated in the database.
Of course, we can also configure seed data before initializing the database.
Interested students can check it on the official websiteSeed data setting
The configuration methods are quite different
image 23.png

Of course, efcore also supports dbfirst. You can generate entities through the existing table structure in the database
Due to the limited space of this article, you can choose to learn about dbfirst on the official website or wait for my subsequent articles.


If there is anything wrong with the article, you are welcome to correct it.
In the future, we will also share articles on the experience of EF core in the project, including stepping on pits and optimization. Welcome to pay attention!

Recommended Today

Tutorial on sending e-mail using net:: SMTP class in Ruby

Simple Mail Transfer Protocol(SMTP)SendE-mailAnd routing protocol processing between e-mail servers. RubyIt provides the connection of simple mail transfer protocol (SMTP) client of net:: SMTP class, and provides two new methods: new and start New takes two parameters: Server name defaults to localhost Port number defaults to 25 The start method takes these parameters: Server – […]