2. ORM (Object/Relational Mapping)
• ORM: a programming technique for converting data
between incompatible type systems in relational
databases and object-oriented programming
languages.
3. why do we need it?
• ORM is a tool for storing data from business objects (Entities, Model Objects,
POCOs) to relational database like MS SQL Server in an automated way
without much programming.
• Automates standard CRUD operation (Create, Read, Update & Delete) so
developer doesn’t need to write it manually.
• Hide the SQL away from your logic code. Leads to easily support more
database engines.
• Less Code, Less Effort, Productivity, simplicity, abstracting your data layer,
and enables you to more focus on app business
4. ORMs for .Net
• NHibernate (open source). 1st release 2007 . Ported from Java Hibernate which started 2001
• LBLGen Pro (commercial). 1st release 2003
• EntitySpaces (open source). 1st release 2006
• Telerik Data Access (free)
• DevExpress XPO (commercial)
• Micro ORMs (less features but optimized for performance):
• PetaPOCO, Dapper, OrmLite
• Entity Framework by Microsoft (open source). 1st release 2008 with .NetFramework 3.5 SP1.
5. Some of ORMs Common Features
• Entity Mapping and Model Creation
• Visual Studio Integration
• LINQ support
• Call Stored Procedures, User Defined Functions, and SQL Table-Valued Functions
• Database Engine Support (SQL Server, Oracle, MySQL, SQLite, SQL CE, …) using ADO.Net Data Providers.
• Support Transactions
• First Level Caching (L1 Caching), and Second Level Caching (L2 Caching).
• Tracking Changes
• Concurrency Control (Optimistic, Pessimistic)
• Lazy Loading
• Batch operations Bulk Insert / Update / Delete
6. Entity Framework
• Open source object-relational mapping (ORM) framework for ADO.NET,
part of .NET Framework.
• Enables developers to work with relational database and deal with data as
objects and properties. Using the EF, developers write queries using LINQ,
then retrieve and manipulate data as strongly typed objects
7. Entity Framework Basic Terminology
• Entities: they represent the individual instances of the objects (such
as customer, orders) to which the information pertains.
• Entity Data Model (EDM): xml file where the logical schema and its mapping with
the physical schema is represented (Generates Context class and Entity classes).
• Context Class: Responsible for Querying: It can be useful in querying the database.
It converts database values into entity objects and vica versa. Change Tracking: It
keeps track of changes occurred in the entities after it has been querying from the
database. Persisting Data: Saving, Updating
• EDM Designer: where you build and manage your EDM
9. Next Session(s)
• EF architecture deeper look.
• Stored Procedures Object Mapping
• Optimistic Concurrency Example using RowVersion.
• Repository and Unit of Work design patterns and how to utilize both on EF
• Very Useful EF Extensions
• Demo Application (featuring grid with searching, paging, sorting, caching, basic
reporting rdlc, and JavaScript charts)