[EntityFramework CodeFirst easy to get started] (1) Make the data persistence of the class easier[EntityFramework CodeFirst easy to get started] (2) Example: Install CodeFirst development environment

table of Contents

related articles

[EntityFramework CodeFirst easy to get started] (1) Make the data persistence of the class easier

[EntityFramework CodeFirst easy to get started] (2) Example: Install CodeFirst development environment

[EntityFramework CodeFirst easy to get started] (3) Example: Create a new CodeFirst entity class

[EntityFramework CodeFirst easy to get started] (4) Example: Create a new CodeFirst database session [DbContext, DbSet]

[EntityFramework CodeFirst easy to get started] (5) Example: Use CodeFirst data session and entity classes

Contents of this chapter

1. Say goodbye to writing cumbersome SQL statements.

2. Manipulate the entity class like writing a Select statement.

3. It is enough to write only the program code.

4. Don't worry too much about performance.


Through the use process, it is found that EF Code First has the following tangible benefits:

1. Say goodbye to writing cumbersome SQL statements.

Sure enough, Entity Framework did not disappoint the .Net developers. In addition to the original Sql Server, Oracle, MySql, Sqlite-more and more databases are being supported one after another. By using the EF architecture, you can easily realize the data conversion between the object-oriented model and the relational database model. In the foreground program, by instantiating an entity class, then reading and writing its properties and calling its methods, EF will automatically generate the corresponding SQL CRUD (add, read, modify, delete) command statement and submit it to the background database. Program developers who are deeply involved in business must agree that he can focus more on the business logic itself, and ignore writing tedious SQL statements to maintain data integrity.

2. Manipulate the entity class like writing a Select statement.

Friends who are familiar with Sql statements know that it is very flexible and simple to write Sql statements to query data from a data table. And EF supports Linq, you can use codes similar to Sql syntax to manipulate entity classes more conveniently. Friends who like to write Sql queries must agree that the select statement has been sublimated in object-oriented.

The sample Linq code for retrieving the entity class is as follows:

using (Company company = new Company()){    var q = from m in company.Department //company.Department 是一个EF实体类            where m.Code == "001"            select m;    if (q.Count() > 0)    {        var m = q.First();    }}

3. It is enough to write only the program code.

Compared with Database First and Model First, the advantage of Code First is that only familiar code files need to be written, no need to build a database in advance, and no need to configure additional mapping model files in a separate format. In most cases, it is sufficient to add a mapping feature mark on the entity class or customize the mapping class. The rest is handed over to Code First, he will automatically create and maintain the database structure. Friends who like to type code must agree, and the data correspondence relationship is clear at a glance.

The sample code for defining the entity class is as follows:

public class Department{     /// <summary>    /// 编码    /// </summary>    [Required] //EF Code First 标记    [StringLength(100)] //EF Code First 标记    public string Code { get; set; }     /// <summary>    /// 名称    /// </summary>    [Required] //EF Code First 标记    [StringLength(200)] //EF Code First 标记    public string Name { get; set; }  }

4. Don't worry too much about performance.

With the continuous iteration of EF and Code First versions, as well as the actual testing of project applications, Code First is stable for general production environments, and the data access speed when the program is running is acceptable, and may be better than the data we wrote ourselves. Access modules must be more consistent and reliable.

Let’s finish here first.

Daring will help the world, while the poor will help the world alone. ——"Mencius·Zhenju I with Heart·Section Ninth"