Entity Framework Core

Hi friends. On this wonderful May day, we continue to work and today we want to talk about the fact that in May OTUS launches a course that everyone likes “C # Developer”as well as a separate course on C # ASP. NET core. Traditionally, on the eve of the start of the courses, we begin to publish useful material. Go.


Introduction

Most modern ASP NET Core applications use the Entity Framework Core. Entity Framework Core is a technology for accessing databases from Microsoft. It allows you to interact with the DBMS using entities, that is, classes and NET objects, rather than database tables. This is the most famous and functional ORM tool in C #. ORM is an object-relational mapping – mapping data to real objects.

For example, if a developer works directly with databases, a programmer should think about connecting, preparing SQL and SQL parameters, how to send queries and transactions. And with the help of the Entity Framework Core, all this is done automatically – the developer works directly with the NET classes.

ORM Approaches

ORM has several approaches.

The first – Code First. It implies that C # code is written first, and then a database is created using this code. For this approach, it is very important to define the classes of the model or entity that will be stored in the database, describe it in C # classes as a model, and write a context class that will work with the database used. The Code First approach is most often used by C # programmers.

Second approach – Database-First- is suitable for those who know SQL well, but in this case it is not necessary to know C # well. The database is created first, then the EDMX database model is generated. This XML in the .edmx file contains information about the structure of the database, the data model, and mapping them to each other. Visual Studio has a graphic designer with which you can work with .edmx

Model-First is third approach ORM. It is often used by architects, since with this approach you can not know either SQL or C # syntax. In this case, a graphical EDMX model is created first, at this time C # model classes are created in the background, and then a database is generated based on the EDMX diagram.

Entity Framework Core Models

All database tables are defined in the Entity Framework as classes of models or entities, as a rule, according to the principle of 1 table, for example users, – 1 class in NET, for example, User. Such pairs are called conventions, and they are defined in the data context class as DbSet sets and this approach works by default.

Although mechanisms exist such as the Fluent API and data annotations, it is possible to override these conventions or additional configuration rules.

Migrations

During the development process, it is quite probable that the Entity Framework model class has changed, and you have to delete the database in order to maintain model compliance. But when you delete the database, all data from it is also deleted.

To save data when a model changes, there is a migration function in the Entity Framework Core. It allows you to consistently apply schema changes to the database in order to synchronize it with the data model.

In migration, there are operations that allow you to delete, add columns and tables, foreign keys, change column settings, add, delete and change data, and so on. When you create a migration, a class is automatically created where the operations that are necessary to apply the Up () migration and return it to the Down () method are performed.

LINQ

LINQ is inextricably linked to the Entity Framework in NET. LINQ is a Language Integrated Query or Intralingual Query – this is a technology that is a set of functions in NET that allow you to write structured database queries.

For work with Entity Framework Core uses technology LINQ to Entities. LINQ uses SQL-like C # expressions to retrieve data from a database. Any relational database works through SQL queries, and the Entity Framework Core translates LINQ to Entities expressions into SQL queries that are understandable for the database used.

Conclusion

Thus, we briefly went over the capabilities of the Entity Framework Core. As you saw, it is really very powerful, and so much so that the programmer who works with it does not even need to know SQL. And Entity Framework Core rightfully holds first place among ORMs in the NET world.



Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *