ENTITY FRAMEWORK IN ASP.
NET
MVC
APPLICATION
OBJECT OBJECT
Ado.Net
SqlConnection
SqlCommand
SqlDataReader
FRAMEWORK SqlDataAdapter
Stored Procedures
Manual Mapping
DATABASE
To address the data access requirements of ASP.NET MVC application, you can
use an ORM (OBJECT RELATIONAL MAPPING) framework.
An ORM framework:
Simplifies the process of accessing data from applications.
ORM is a tool for storing data from domain objects to relational database.
ENTITY FRAMEWORK:
The Entity Framework is an ORM framework that ASP.NET MVC
applications can use.
The Entity Framework is an implementation of the Entity Data Model
(EDM), which is a conceptual model that describes the entities and the
associations they participate in an application.
The Entity Data Model (EDM) is a set of concepts that describe the
structure of data.
Entity Data Model is a model that describes entities and the relationships
between them.
EDM allows you to handle data access logic by programming against
entities without having to worry about the structure of the underlying data
store and how to connect with it.
Entity Framework eliminates the need to write most of the data-access
code that otherwise need to be written.
It is an enhancement to ADO.NET that gives developers an automated
mechanism for accessing and storing the data in the database.
Using the Entity Framework, developers issue queries using LINQ (Language
integrated query), then retrieve and manipulate data as strongly type
objects.
Uses different approaches to manage data related to an application.
o CODE FIRST APPROACH
o DATABASE FIRST APPROACH
o MODEL FIRST APPROACH
CODE-FIRST APPROACH OF ENTITY
FRAMEWORK IN MVC
In the code-first approach the Entity Framework creates database objects based
on model classes that you create to represent application data.
THE CODE-FIRST APPROACH:
Is the most common approach implemented in ASP.NET MVC Framework.
Allows you to develop your application by coding model classes and
properties and delegate the process of creating the database objects to the
Entity Framework.
The code-first approach allows you to define your own model by creating
custom classes.
Then, you can create database based on the models.
Following figure shows the code-first approach:
The DbContext class:
Is provided by the System.Data.Entity namespace of the ASP.NET MVC
Framework.
Can be used to define the database context class after creating a model
class.
Coordinates with Entity Framework and allows you to query and save the
data in the database.
Uses the DbSet <T> type to define one or more properties where, T
represents the type of an object that needs to be stored in the database.
CREATING CRUD APPLICATION USING ENTITY
FRAMEWORK CODE FIRST APPROACH IN ASP.NET MVC
C – CREATE --- INSERT DATA INTO DATABASE
R – READ -- SELECT DATA FROM DATABASE
U – UPDATE --- UPDATE DATA IN DATABASE
D – DELETE --- DELETE DATA FROM DATABASE
DATABASE-FIRST APPROACH OF
ENTITY FRAMEWORK IN ASP.NET
MVC 5
In the database-first approach the Entity Framework creates model classes
and properties corresponding to the existing database objects, such as
tables and columns.
The database-first approach is applicable in scenario where a database
already exists for the application.
Following figure shows the database-first approach: