Chương 08 Entity Framework
Chương 08 Entity Framework
ASP.NET MVC
Entity Framework
• Is an ORM Framework (Object Relational Mapper):
translates domain classes into rows and columns in the
database tables.
• Sits between your application and the data store.
• The applications use the Entity framework API for the
database related operations. The Entity Framework
maps all the database related operations to the
database.
Entity Framework
The EF
uses ADO.NET
to perform the
operations on
the database
Benefits
• Developers spend lots of time coding the plumbing required to save
the data to the data store. EF reduces this time, hence the
developers can spend time actually building the application.
• Developers can work against domain specific objects such as
employee and employee address and need not to worry about how
and where these data is stored.
• Applications are now easier to maintain as the amount of code is
reduced. Reduced code also increase the productivity.
• You don’t have to write SQL queries. EF will take care of that. It is
smart enough to know the associates between tables and
generates the join queries.
• Advanced relationships like inheritance, associations can be set up
between domain models to suit the need of the application.
Terms
• Entity: The Entity is defined as an object with independent
existence. For example, an employee in working for a
company is an entity.
• Properties: Each Entity has properties or attributes similar to
class. For example, in the case of employee entity, his name,
joining date and address are Properties. The Properties can be
int, string, collections or of a complex type.
• Entity Type: The Entity type is a collection of entities that
share the common properties. It represents a unique object in
your domain model. The Entity is a single instance of Entity
Type.
Entity Data Model
Entity Data Model (EDM) is the heart of our Entity
framework. The Entity Data model describes the Conceptual
model of our domain objects. It enables you to create
strongly typed domain classes (Entity types). It defines
associations between these domain classes. It then maps
these classes to the database Schema.
Entity Framework Architecture
Conceptual Model
Object Services
Mapping
Entity Object Data Provider
Storage Model
ADO.Net Data Provider
Database
Entity Framework
Database First Model First Code First
Database Model Code
Model Code
Model
First?
Code
First?
Model First
• In this approach database models are created First. The
models are created using EDMX (Entity Data Model
Designer) Designer.
• Entity Framework then uses the .EDMX, which is generated
by the tool to generate the database from the model.
Database First
• In this approach EDMX is created from the existing database.
This is the reverse of the model first approach where models
are created first and database later. The Entity Data Model
can be updated whenever database schema changes. Also,
database-first approach supports stored procedure, view,
etc.
• Database first is is used if you have Database is developed
separately or if you have an existing DB. Any changes made
to the database can be easily updated in the model from the
database.
Code First
• Code first gives full control over the code to the developer.
There is no auto generated code as in the case of the database
first or mode first.
• In code first you are more responsible for the database schema.
You have to configure the relationships, constraints, and
associations between entities.
• Adding a custom field in auto generated models, you may need
to extend the model class. This is not a problem in the code first
as you can add it to the existing class.
• Code first can be used in both existing or new databases. In
case of an existing database, you may have to manually code
model classes to match the database. The database first
approach will create all the domain classes for you.
• Database first/Model first is going to be retired in
Entity framework Version 7.
• In the EF 7 .EDMX file is discarded. Hence code first
workflow is the only way going forward.
Conclusion