0% found this document useful (0 votes)
12 views33 pages

Chương 08 Entity Framework

Uploaded by

nguyendamkha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views33 pages

Chương 08 Entity Framework

Uploaded by

nguyendamkha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

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

Entity Data Model LINQ to Entities Entity SQL

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

Code Database Database


Database
First?

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

The Entity framework allows us to create the model


directly using the code, which is called Code First.
DEMO
ASP.NET MVC Entity Framework
Database First
Code First
Model First
DB first
Bước 1: Tạo cơ sở dữ liệu : Data => Add New Item => Sql Sever Database.
DB first
Bước 2 : tạo các table Student và Class từ Server Explorer
DB first
• Tạo table với các thuộc tính và ràng buộc như sau:
DB first
• Tạo table với các thuộc tính và ràng buộc như sau:
DB first
• Bước 3 : Thiết lập Entity Data Model dựa trên CSDL có sẵn: Model => Add=>New Item=>
Data => ADO.NET Entity Data Model
DB first
DB first
DB first
DB first
DB first
• Bước 4: Tạo Controller xử lý cho dữ liệu Student và Class
DB first
• Bước 4: Tạo Controller xử lý cho dữ liệu Student và Class
DB first
• Controller cho Student
DB first
• Controller cho Class
DB first
• Sau khi add xong ta sẽ thêm 2 Controller như sau và các Views tương ứng
Kết quả
Kết quả
Question #1
• DbContext là gì?

Question #2
• The DbSet class là gì?

You might also like