Full Stack Developer
Entity Framework is an open-source object-relational mapper framework for .NET applications supported by Microsoft. It increases the developer’s productivity as it enables developers to work with data using objects of domain-specific classes without focusing on the underlying database tables and columns where this data is stored. It eliminates the need for most of the data-access code which is used to interact with the database that developers usually need to write. It provides an abstract level to the developers to work with a relational table and columns by using the domain-specific object. It also reduces the code size of the data-specific applications and also the readability of the code increases by using it. This is a new technology for accessing the data for Microsoft applications. The latest version for Entity Framework is 6.0.
The following figure describes where the Entity Framework is present in your application.
The above figure represents how an entity framework interacts with the domain class and database. It provides a connection between the business entity and data tables in the database. It saves data stored in the properties of business entities and also retrieves data from the database and converts it to business entities objects automatically. Entity Framework will execute the relevant query in the database and then materialize results into instances of your domain objects for you to work within your app.
Cross-platform:
EF Core is a cross-platform framework that can run on Windows, Linux, and Mac.
Querying:
EF allows us to use LINQ queries (C#/VB.NET) to retrieve data from the underlying database. The database provider will translate these LINQ queries to the database-specific query language (e.g. SQL for a relational database). EF also allows us to execute raw SQL queries directly to the database.
Change Tracking:
EF keeps track of changes that occurred to instances of your entities (Property values) that need to be submitted to the database.
Transactions:
EF performs automatic transaction management while querying or saving data. It also provides options to customize transaction management.
Built-in Conventions:
EF follows conventions over the configuration programming pattern and includes a set of default rules which automatically configure the EF model.
Migrations:
EF provides a set of migration commands that can be executed on the NuGet Package Manager Console or the Command Line Interface to create or manage underlying database Schema.
Modeling:
EF (Entity Framework) creates an EDM (Entity Data Model) based on POCO (Plain Old CLR Object) entities with getting/set properties of different data types. It uses this model when querying or saving entity data to the underlying database.
Saving:
Entity Framework executes the INSERT, UPDATE, and DELETE commands to the database based on the changes that occurred to the entities when we call the "SaveChanges()" method. Entity Framework also provides the asynchronous "SaveChangesAsync()" method.
Concurrency:
EF uses Optimistic Concurrency by default to protect overwriting changes made by another user since data was fetched from the database.
Caching:
EF includes the first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
Configurations:
EF allows us to configure the EF model by using data annotation attributes or Fluent API to override default conventions.
Microsoft introduced Entity Framework in 2008 with .NET Framework 3.5. Since then, it released many versions of Entity Framework. Currently, there are two latest versions of Entity Framework: EF 6 and EF Core. The following table lists the important difference between EF 6 and EF Core.
Microsoft provides a free version of visual studio which also contains SQL Server. It can be downloaded from virtual studio website.
Step 1 − Once downloading is complete, run the installer. The following dialog will be displayed.
Step 2 − Click on the Install button and it will start the installation process.
Step 3 − Once the installation process is completed successfully, you will see the following dialog. Close this dialog and restart your computer if required.
Step 4 − Open Visual Studio from Start Menu which will open the following dialog. It will be a while for the first time for preparation.
Step 5 − Once all is done you will see the main window of Visual studio.
Let’s create a new project from File → New → Project
Step 1 − Select Console Application and click the OK button.
Step 2 − In solution Explorer, right-click on your project.
Step 3 − Select Manage NuGet Packages as shown in the above image, which will open the following window in Visual Studio.
Step 4 − Search for Entity Framework and install the latest version by pressing the install button.
Step 5 − Click Ok. Once installation is done, you will see the following message in your output window.
You are now ready to start your application.