MVCEntityFramework.md - brainchildservices/curriculum GitHub Wiki
Slide 1
ASP.NET MVC Entity Framework
Prior to .NET 3.5, we (developers) often used to write ADO.NET code or Enterprise Data Access Block to save or retrieve application data from the underlying database. We used to open a connection to the database, create a DataSet to fetch or submit the data to the database, convert data from the DataSet to .NET objects or vice-versa to apply business rules. This was a cumbersome and error prone process. Microsoft has provided a framework called "Entity Framework" to automate all these database related activities for your application.
Slide 2
Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. 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. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code compared with traditional applications.
Slide 3
Official Definition: “Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.”
The following figure illustrates where the Entity Framework fits into your application.
Slide 4
As per the above figure, Entity Framework fits between the business entities (domain classes) and 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.
Slide 5
Entity Framework Features
- Cross-platform: EF Core is a cross-platform framework which can run on Windows, Linux and Mac.
- Modelling: EF (Entity Framework) creates an EDM (Entity Data Model) based on POCO (Plain Old CLR Object) entities with get/set properties of different data types. It uses this model when querying or saving entity data to the underlying database.
- Querying: EF allows us to use LINQ queries (C#/VB.NET) to retrieve data from the underlying database. The database provider will translate this 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 occurred to instances of your entities (Property values) which need to be submitted to the database.
- Saving: EF executes INSERT, UPDATE, and DELETE commands to the database based on the changes occurred to your entities when you call the SaveChanges() method. EF also provides the asynchronous SaveChangesAsync() method.
Slide 5 Downwards
- Concurrency: EF uses Optimistic Concurrency by default to protect overwriting changes made by another user since data was fetched from the database.
- Transactions: EF performs automatic transaction management while querying or saving data. It also provides options to customize transaction management.
- Caching: EF includes first level of caching out of the box. So, repeated querying will return data from the cache instead of hitting the database.
- Built-in Conventions: EF follows conventions over the configuration programming pattern, and includes a set of default rules which automatically configure the EF model.
- Configurations: EF allows us to configure the EF model by using data annotation attributes or Fluent API to override default conventions.
- 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.
Slide 6
Install Entity Framework 5
You need to install EntityFramework.dll (EF API) in your project in Visual Studio in order to work with the Code-First approach.
Here, we will install Entity Framework 5 API using NuGet Package Manager in Visual Studio. (You can install EF via NuGet the same way in any version of Visual Studio.)
Right click on your project in the solution explorer and select Manage NuGet Packages
Slide 7
or Click on Tools>NuGet Package Manager>Manage NuGet Packages for Solutions...
Slide 8
This will open the Manage NuGet Packages dialogue box. Search for EntityFramework
in the top left search box and press Enter. It will display all the API/plug-ins starting with EntityFramework
as shown below.
Slide 9
Select EntityFramework (make sure that the author is Microsoft and the version is 6.x) and click on Install. This will open the preview box as shown below. Review the changes and click OK.
Slide 10
This will install Entity Framework API in the project.
Slide 11
After installation, make sure that the appropriate version of EntityFramework.dll is included in the project.
Slide 12
Also install another extension called Microsoft.EntityFrameworkCoree.SqlServer
same as above
To install using VS Code Please Visit:- EF Core CLI Commands