Using the Entity Framework

Both approaches have their benefits, but the code-first approach is more popular among developers as you have to deal less with the database and work more in C#.

An EF doesn't comes with the .NET framework by default. You have to download the library from the NuGet package manager and install it in the project you are working with. To download and install the entity framework, you can open the Nuget Package Manager Console and write the following command:

Install-Package EntityFramework

This command will download and install Entity Framework in your project:

If you are not comfortable with the Package Manager Console, you can also use the GUI's Manage Packages for Solution window to install entity framework. Go to the Browse tab and search for Entity Framework. You will see it at the top of the search results. Click it and install it in your project:

Installing Entity Framework using Nuget Package Manager

In this book, we are focusing more on C#, so we will look more closely at the code-first approach than the database-first approach. In the code-first approach, as we don't touch the database code, we need to make our entity objects in a way that can be followed when creating a database. After we have created the database tables, if we want to update the tables or change the tables, we need to use migrations. Database migration creates a new instance of the database and applies the new changes in the new instance. By using migrations, it's easier to manipulate the database.

Let's now learn a little bit more about the history and the flow of EF. It was first published in the year 2008 with .NET 3.5. At the time of writing this book, the latest version of EF is version 6. EF also has a .NET Core version that is called Entity Framework Core. Both frameworks are open source. When you install an entity framework in your project and write a Plain Old CLR Object (POCO) class, that POCO class is used by the entity framework. First, EF creates an Entity Data Model (EDM) from it. This EDM is used later to save and query in the database. Language Integrated Queries (LINQs) and SQL can both be used to give instructions to EF. When one entity object is used in EDM, it is tracked. When it is updated, the database will also be updated.

We can use the SaveChanges() method to execute Insert, Update, and Delete activity in the database. For asynchronous programming, the SaveChangesAsync() method is used. For a better query experience, EF has first-level caching, so when repeated queries are executed, EF returns the results from the cache instead of going to the database to collect the same result.

An EF API mainly does four things: 

  • Maps classes to the database schema
  • Translates LINQs to Entity Queries to SQL and executes them
  • Tracks changes
  • Saves changes in the database

EF converts entity objects and context classes to EDM, and EDM is used in the Database. For example, let's say we have the following class:

public class Person {
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

The EF will convert it to EDM, which looks as follows:

Table Name: Person
PersonId(PK,int,not null)
FirstName (nvarchar(50),null)
LastName (nvarchar(50),null)

Then, this EDM will be used to create or update the Person database table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.42.129