0% found this document useful (0 votes)
26 views29 pages

Orm 3

Object Relational Mapping (ORM) is a programming technique that converts data between object-oriented programming languages and relational databases, allowing developers to work with data in an object-oriented manner. Entity Framework (EF) is a specific ORM framework for .NET applications that simplifies data access and integrates seamlessly with the .NET ecosystem. The document also discusses various approaches to using EF, including Database First, Code First, and Model First, along with the characteristics of POCO and Dynamic Proxy entities.

Uploaded by

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

Orm 3

Object Relational Mapping (ORM) is a programming technique that converts data between object-oriented programming languages and relational databases, allowing developers to work with data in an object-oriented manner. Entity Framework (EF) is a specific ORM framework for .NET applications that simplifies data access and integrates seamlessly with the .NET ecosystem. The document also discusses various approaches to using EF, including Database First, Code First, and Model First, along with the characteristics of POCO and Dynamic Proxy entities.

Uploaded by

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

Object Relational Mapping

(ORM)
Lecture 01
Introduction
● What is ORM: ORM is a programming technique that facilitates the
conversion of data between incompatible type systems in object-
oriented programming languages and relational databases.
● Purpose: It aims to bridge the gap between the object-oriented
programming paradigm and the relational database model, allowing
developers to work with database data in an object-oriented manner.
● ORM Frameworks: Examples include Hibernate for Java, Entity
Framework for .NET, SQLAlchemy for Python, and Doctrine for PHP.
Components of ORM
● Domain Class Objects: These are the object-oriented representations of
entities within the application's domain. For instance, in a library application,
classes like Book, Author, and Library could be domain class objects.
● Relational Database Objects: These are the tables, rows, and columns in a
relational database that store the data. Each domain class object typically
corresponds to a table in the database.
● Mapping Information: ORM frameworks use mapping information to
establish the relationships between domain class objects and relational
database objects. This mapping defines how data is persisted and retrieved.
Introduction to Entity Framework (EF)
● Entity Framework (EF): EF is an ORM framework developed by
Microsoft for .NET applications. It enables developers to work with
data in the form of domain class objects rather than writing complex
SQL queries.
● Integration with .NET: EF seamlessly integrates with the .NET
ecosystem, providing a set of tools and libraries for database
interactions.
● Advantages: Simplifies data access, reduces boilerplate code,
supports multiple database providers (SQL Server, MySQL, SQLite,
etc.).
Layers in Application
Architecture
● Presentation Layer: This layer handles user interactions and
displays information to the user. It includes components like UI
controls, views, and controllers.
● Business Logic Layer: Contains the business rules and logic of the
application. It orchestrates data processing and interacts with the
data access layer.
● Data Access Layer (DAL): EF resides in this layer, abstracting the
interaction with the database. It provides functionalities for querying,
inserting, updating, and deleting data without directly manipulating
SQL.
Where Entity Framework Fits ?
Entity Framework Main Classes
● DbContext Class (represents database): The DbContext class is the
entry point for using EF. It represents a session with the database and
provides APIs for querying and saving data. It also used to configure
domain classes, database related mappings, change tracking settings,
transactions etc.
● Entity (represents tables): An entity is a class that represents a
database table or view. Each instance of an entity class corresponds to a
row in the table, and its properties map to columns. Use to represent an
entity set that is used for create, read, update and delete operations.
Entity Framework Components
Properties of Entity class: Properties of an entity class define the
attributes of the corresponding database table's columns.

Entity can have two types of properties :

1. Scalar properties : Properties that stored actual data. It maps to to


single column in the database table.
2. Navigation properties :Represents a relationship to another entity.
There are two types Navigation properties : reference and collection
navigation according to the association of data
Entity Framework Components
Types of Entity
1. Plain Old CLR (common language runtime) Object (POCO) Entity
● POCO entities are simple .NET classes that represent entities in the
application's domain model.
● Characteristics:
● POCO entities do not inherit from any framework-specific base class.
● They are plain C# or VB.NET classes with properties representing entity
attributes.
● POCO entities are typically used in Code First and Model First
approaches of Entity Framework.
public class Customer

public int Id { get; set; }

public string Name { get; set; }

public string Email { get; set; }

}
2. Dynamic Proxy (POCO Proxy) Entity
● Dynamic proxy entities are runtime-generated subclasses of POCO
entities, created by Entity Framework to enable lazy loading,
change tracking, and proxy-based behavior.
● Characteristics:
● Dynamic proxy entities are created when lazy loading or
change tracking is enabled in Entity Framework.
● They inherit from the original POCO entity class and override
virtual properties to add behavior.
● Dynamic proxy entities enable features like lazy loading of
navigation properties and change tracking without modifying
the original entity class.
Dynamic proxy entity class (generated by Entity Framework):

public class CustomerProxy : Customer


{
public override string Name
{
get { return base.Name; }
set { base.Name = value; }
}

// Additional behavior added by Entity Framework for change


tracking, lazy loading, etc.
}
POCO entity should meet the following requirements to
become a POCO proxy….

1. A POCO class must be declared with public access


2. A POCO class must not be sealed
3. A POCO class must not be abstract
4. Each navigation property must be declared as public virtual
5. Each navigation collection property must be Collection<T>
6. ProxyCreationEnabled option must NOT be false (defaut is true in context
class)
Navigation Properties:

public class Customer

public int Id { get; set; }

public virtual ICollection<Order> Orders { get; set; } // Navigation property

}
ProxyCreationEnabled Option

public class YourDbContext : DbContext


{
public YourDbContext() : base("YourConnectionString")
{
// By default, ProxyCreationEnabled is true
this.Configuration.ProxyCreationEnabled = true;
}

// DbSet properties and other configurations


}
Entity Framework Scenarios
● Database First Approach: In this scenario, developers start with an existing
database schema, and EF generates domain class objects and mapping
information based on that schema.
● Code First Approach: Developers define domain class objects and their
relationships in code, and EF generates the database schema based on
these definitions.
● Model First Approach: Developers create a conceptual model using Entity
Framework Designer, and EF generates both the database schema and
domain class objects based on this model.
Database First Scenario
● Steps Involved: Reverse engineering the database schema,
generating domain class objects and mapping configurations, and
configuring the context to work with the database.
● Advantages: Rapid development, easy integration with existing
databases.
● Disadvantages: Less control over domain class objects, may lead to
a tightly coupled application.
Code First Scenario
● Steps Involved: Defining domain class objects and their relationships
using code-first conventions or fluent API, configuring the context,
and generating or updating the database schema.
● Advantages: Full control over domain class objects, supports
migrations for database schema changes.
● Disadvantages: Initial setup may require more effort, especially for
complex models.
Model First Scenario
● Steps Involved: Creating a conceptual model using Entity
Framework Designer, generating database schema and domain class
objects from the model, and configuring the context to work with the
database.
● Advantages: Visual modeling, rapid prototyping, and easy
modification of the conceptual model.
● Disadvantages: Limited control over database schema generation,
reliance on visual design tools.
REFERENCES
● Entity Framework Core in Action by Jon Smith, Andrew Lock, and Eric J. Smith

● Programming Entity Framework: Building Data Centric Apps with the ADO.NET Entity
Framework by Julia Lerman

● What is ORM tool by Telusko : https://youtu.be/wIp3veR_9Ag?si=zyjvhR573uQQRVte

You might also like