ADO.NET Entity                                 Framework                              ORM Concepts, ADO.NET Entity        ...
Table of Contents ORM Technologies   – Basic Concepts Entity Framework  Overview  Printing the native SQL queries  LI...
Table of Contents (2) Executing Native SQL Queries   Paramerterless Queries   Parameterized Queries The N+1 Query Prob...
Introduction to ORMObject-Relational Mapping (ORM) Technologies
ORM Technologies Object-Relational Mapping (ORM) is a programming technique for automatic mapping and converting data   ...
ORM Frameworks ORM frameworks typically     provide the following functionality:  Creating object model by database sche...
ORM Mapping – Example Database and Entities mapping diagrams   for a subset of the Northwind database      Relational    ...
ORM Advantages Object-relational mapping advantages  Developer productivity    Writing less code  Abstract from differ...
Approaches to ORM Template-based code generation vs.     entity classes mappings SQL generation (design time / runtime) ...
Object Persistence Frameworks Code generation tools   Generate C#, XML and other files   Source code is compiled and us...
ORM Frameworks in .NET Built-in ORM tools   in .NET Framework and VS   ADO.NET Entity Framework   LINQ-to-SQL   Both c...
ADO.NET Entity Framework  Object Relation Persistence Framework
Overview of ADO.NET EF Entity Framework (EF) is a standard ORM framework, part of .NET   Provides a run-time infrastruct...
Overview of ADO.NET EF (2) Entity Framework provides                          an application programming interface (API) ...
Entity Data Model The Entity Data Model (EDM) is a schema language for entities, consisting of:  Conceptual model (CSDL)...
Entity Framework Architecture                            16
Entity Framework Features Entity Framework (EF) standard   features:  Maps tables, views, stored procedures and   functi...
Entity Framework Lifecycle When the application   starts  EF translates into SQL the language-integrated   queries in th...
Entity Framework Lifecycle (2) When the database     returns the results   Entity Framework translates the database rows...
EF Components The ObjectContext class   ObjectContext holds the database connection    and the entity classes   Provide...
EF Components (2) Associations   An association is a primary key / foreign key    based relationship between two entity ...
LINQ to Entity Files (.edmx) Visual Studio has built-in Entity Framework data designer and code generator   Mappings are...
LINQ to Entity Files – Example EDMX mapping for the Categories table from Northwind database in SQL Server<EntityType Nam...
The Entity FrameworkDesigner in Visual Studio         Live Demo
The ObjectContext Class The ObjectContext class    is generated by the Visual Studio designer ObjectContext provides:  ...
Using ObjectContext Class First create instance of the     ObjectContext:  NorthwindEntities northwind = new NorthwindEnt...
Reading Data with LINQ Query   Executing LINQ-to-Entities query over EF entity:    NorthwindEntities context = new Northw...
Logging the Native SQL Queries To print        the native database SQL commands executed on the server use the following:...
Retrieving Data with  LINQ to Entities       Live Demo
Creating New Data To create a new database row use the method AddObject(…) of the corresponding collection:  // Create ne...
Creating New Data (2) Creating new row can alsobe done by using the AddTo + The_Entity_Name method directly on the Object...
Cascading Inserts We can also   add cascading entities to the database: Country spain = new Country(); spain.Name = "Spai...
Updating Existing Data ObjectContext allows   modifying entity properties and persisting them in the database  Just load...
Deleting Existing Data Delete is done by DeleteObject() on the specified entity collection SaveChanges() method performs...
CRUD Operations with  Entity Framework       Live Demo
Executing Native   SQL QueriesParameterless and Parameterized
Executing Native SQL Queries Executing a native SQL query in Entity Framework directly in its database store:  ctx.Execut...
Executing Native SQL Queries (2) Native SQL queries can also       be parameterized:  NorthwindEntities context = new Nor...
Executing Native  SQL Queries     Live Demo
The N+1 Query ProblemWhat is the N+1 Query Problem and How to Avoid It?
The N+1 Query Problem What is the N+1 Query Problem?  Imagine a database that contains tables   Customers and Orders   ...
The N+1 Query Problem (2)                  A single query to retrieve the countries This code will execute N+1 DB queries...
Solution to the N+1 Query                                   Problem Fortunately there is                    an easy way i...
Solution to the N+1  Query Problem      Live Demo
Joining andGrouping TablesJoin and Group Using LINQ
Joining Tables in EF In EF we can join tables                       in LINQ or by using extension methods on IEnumerable<...
Grouping Tables in EF Grouping also      can be done by LINQ     The same ways as with collections in LINQ   Grouping w...
Joining andGrouping Tables    Live Demo
Attaching andDetaching Objects
Attaching and Detaching                                   Objects In Entity Framework, objects can be attached to or deta...
Attaching Detached Objects When a query is executed inside an ObjectContext, the returned objects are automatically attac...
Detaching Objects When an object is detached?  When we obtain the object from an   ObjectContext and the Dispose it  Ma...
Attaching Objects When we want to update a detached object we need to reattach it and the update it  Done by the Attach(...
Attaching andDetaching Objects     Live Demo
Using Transactions in EF Just Use the TransactionScope Class
Using Transactions in EF To perform transactional                       logic, just use the TransactionScope class   You...
Transactions in EF      Live Demo
ADO.NET Entity Framework    курсове и уроци по програмиране, уеб дизайн – безплатно     BG Coder - онлайн състезателна сис...
Exercises1.   Using the Visual Studio Entity Framework designer     create a ObjectContext for the Northwind     database2...
Exercises (2)6.   Create a database called NorthwindTwin with the     same structure as Northwind using the features from ...
Exercises (3)9.    Create a method that places a new order in the      Northwind database. The order should contain      s...
Exercises (4)11.   Create a database holding users and groups. Create      a transactional EF based method that creates an...
Free Trainings @ Telerik Academy "C# and Databases       " course @ Telerik    Academy       academy.telerik.com/…csharp...
Upcoming SlideShare
Loading in...5
×

11. ADO.NET Entity Framework - C# and Databases

5,216

Published on

ADO.NET Entity Framework - ORM Concepts, ADO.NET Entity Framework (EF), ObjectContext
Telerik Software Academy: http://academy.telerik.com/school-academy/meetings/details/2012/01/06/desktop-applications-csharp-databases
The website and all video materials are in Bulgarian.
ORM Technologies – Basic Concepts
Entity Framework
Overview
Printing the native SQL queries
LINQ Components
Entity Files
The Visual Studio Designer
ObjectContext Class and CRUD Operations
Executing Native SQL Queries
Paramerterless Queries
Parameterized Queries
The N+1 Query Problem
Joining and Grouping Entities
Attaching and Detaching Objects

Published in: Education, Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,216
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
524
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

11. ADO.NET Entity Framework - C# and Databases

  1. 1. ADO.NET Entity Framework ORM Concepts, ADO.NET Entity Framework (EF), ObjectContextDoncho MinkovTechnical Trainerhttp://www.minkov.itTelerik Software Academyhttp://academy.telerik.com/
  2. 2. Table of Contents ORM Technologies – Basic Concepts Entity Framework  Overview  Printing the native SQL queries  LINQ Components  Entity Files  The Visual Studio Designer  ObjectContext Class and CRUD Operations 2
  3. 3. Table of Contents (2) Executing Native SQL Queries  Paramerterless Queries  Parameterized Queries The N+1 Query Problem Joining and Grouping Entities Attaching and Detaching Objects 3
  4. 4. Introduction to ORMObject-Relational Mapping (ORM) Technologies
  5. 5. ORM Technologies Object-Relational Mapping (ORM) is a programming technique for automatic mapping and converting data  Between relational database tables and object- oriented classes and objects ORM creates a “virtual object database“  Which can be used from within the programming language, e.g. C# or Java ORM frameworks automate the ORM process  A.k.a. object-relational persistence frameworks 5
  6. 6. ORM Frameworks ORM frameworks typically provide the following functionality:  Creating object model by database schema  Creating database schema by object model  Querying data by object-oriented API  Data manipulation operations  CRUD – create, retrieve, update, delete ORM frameworks automatically generate SQL to perform the requested data operations 6
  7. 7. ORM Mapping – Example Database and Entities mapping diagrams for a subset of the Northwind database Relational ORM Entities database schema (C# Classes) ORM Framework 7
  8. 8. ORM Advantages Object-relational mapping advantages  Developer productivity  Writing less code  Abstract from differences between object and relational world  Complexity hidden within ORM  Manageability of the CRUD operations for complex relationships  Easier maintainability 8
  9. 9. Approaches to ORM Template-based code generation vs. entity classes mappings SQL generation (design time / runtime) vs. mapping existing SQL Entity classes representation  Entities are just POCO (Plain Old C# Objects)  Entities implement special IPersistent interface or extend PersistentBase class Configuring mappings  DB schema data vs. XML vs. annotations 9
  10. 10. Object Persistence Frameworks Code generation tools  Generate C#, XML and other files  Source code is compiled and used as API  Can be highly customized Object-relational mapping tools  Mappings are described in XML files or built in the classes as attributes  No source code generation  Use of single standard API 10
  11. 11. ORM Frameworks in .NET Built-in ORM tools in .NET Framework and VS  ADO.NET Entity Framework  LINQ-to-SQL  Both combine entity class mappings and code generation, SQL is generated at runtime Third party ORM tools  NHibernate – the old daddy of ORM  Telerik OpenAccess ORM 11
  12. 12. ADO.NET Entity Framework Object Relation Persistence Framework
  13. 13. Overview of ADO.NET EF Entity Framework (EF) is a standard ORM framework, part of .NET  Provides a run-time infrastructure for managing SQL-based database data as .NET objects The relational database schema is mapped to an object model (classes and associations)  Visual Studio has built-in tools for generating Entity Framework SQL data mappings  Data mappings consist of C# classes and XML  A standard data manipulation API is provided 13
  14. 14. Overview of ADO.NET EF (2) Entity Framework provides an application programming interface (API)  For accessing data stored in database servers  Built on the top of ADO.NET and LINQ LINQ to Entities is Microsoft’s entry-level LINQ-enabled ORM implementation for database servers  Works with SQL Server and SQL Server Express  Could work with MySQL, SQLite, Oracle, etc.  Maps tables and one-to-many and many-to- many relationships 14
  15. 15. Entity Data Model The Entity Data Model (EDM) is a schema language for entities, consisting of:  Conceptual model (CSDL)  Mapping (MSL)  Storage Model (SSDL) 15
  16. 16. Entity Framework Architecture 16
  17. 17. Entity Framework Features Entity Framework (EF) standard features:  Maps tables, views, stored procedures and functions as .NET objects  Provides LINQ-based data queries  Executed as SQL SELECTs on the database server  CRUD operations – Create/Read/Update/Delete  Create compiled queries – for executing the same parameterized query multiple times  Creating or deleting the database schema 17
  18. 18. Entity Framework Lifecycle When the application starts  EF translates into SQL the language-integrated queries in the object model  Sends them to the database for later execution 18
  19. 19. Entity Framework Lifecycle (2) When the database returns the results  Entity Framework translates the database rows back to .NET objects The database server is transparent, hidden behind the API LINQ is executed over IQueryable<T>  At compile time a query expression tree is emitted  At runtime SQL is generated and executed 19
  20. 20. EF Components The ObjectContext class  ObjectContext holds the database connection and the entity classes  Provides LINQ-based data access  Implements identity tracking, change tracking, and API for CRUD operations Entity classes  Each database table is typically mapped to a single entity class (C# class) 20
  21. 21. EF Components (2) Associations  An association is a primary key / foreign key based relationship between two entity classes  Allows navigation from one entity to another, e.g. Student.Courses Concurrency control  Entity Framework uses optimistic concurrency control (no locking by default)  Provides automatic concurrency conflict detection and means for conflicts resolution 21
  22. 22. LINQ to Entity Files (.edmx) Visual Studio has built-in Entity Framework data designer and code generator  Mappings are stored in .edmx files (Entity Data Model XML)  <database>.edmx is an XML file  Holds metadata representing the database schema (CSDL, MSL and SSDL models)  <database>.Designer.cs file contains the C# entity classes and the ObjectContext class  One entity class for each mapped database table 22
  23. 23. LINQ to Entity Files – Example EDMX mapping for the Categories table from Northwind database in SQL Server<EntityType Name="Categories"> <Key> <PropertyRef Name="CategoryID" /> </Key> <Property Name="CategoryID" Nullable="false" Type="int" StoreGeneratedPattern="Identity" /> <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" /> <Property Name="Description" Type="ntext" /> <Property Name="Picture" Type="image" /> Entity class Category</EntityType> 23
  24. 24. The Entity FrameworkDesigner in Visual Studio Live Demo
  25. 25. The ObjectContext Class The ObjectContext class is generated by the Visual Studio designer ObjectContext provides:  Methods for accessing entities (object sets) and creating new entities (AddTo… methods)  Ability to manipulate database data though entity classes (read, modify, delete, insert)  Easily navigate through the table relationships  Executing LINQ queries as native SQL queries  Create the DB schema in the database server 25
  26. 26. Using ObjectContext Class First create instance of the ObjectContext: NorthwindEntities northwind = new NorthwindEntities(); In the constructor you can pass a database connection string and mapping source ObjectContext properties  Connection – the SqlConnection to be used  CommandTimeout – timeout for database SQL commands execution  All entity classes (tables) are listed as properties  e.g. ObjectSet<Order> Orders { get; } 26
  27. 27. Reading Data with LINQ Query Executing LINQ-to-Entities query over EF entity: NorthwindEntities context = new NorthwindEntities(); var customers = from c in context.Customers The query will be executes as where c.City == "London" SQL command in the database select c; Customers property in the ObjectContext: public partial class NorthwindEntities : ObjectContext { public ObjectSet<Customer> Customers { get { … } } } 27
  28. 28. Logging the Native SQL Queries To print the native database SQL commands executed on the server use the following: var query = context.Countries; Console.WriteLine((query as ObjectQuery).ToTraceString()); This will print the SQL native query executed at the database server to select the Countries  Can be printed to file using StreamWriter class instead of Console class 28
  29. 29. Retrieving Data with LINQ to Entities Live Demo
  30. 30. Creating New Data To create a new database row use the method AddObject(…) of the corresponding collection: // Create new order object Order order = new Order() { OrderDate = DateTime.Now, ShipName = "Titanic", ShippedDate = new DateTime(1912, 4, 15), ShipCity = "Bottom Of The Ocean" }; // Mark the object for inserting context.Orders.AddObject(order); This will execute context.SaveChanges(); an SQL INSERT SaveChanges() method call is required to post the SQL commands to the database 30
  31. 31. Creating New Data (2) Creating new row can alsobe done by using the AddTo + The_Entity_Name method directly on the ObjectContext  This method is depricated  Better use the other one // Mark the object for inserting context.AddToOrders(order); // Post changes to database (execute SQL INSERTs) context.SaveChanges(); 31
  32. 32. Cascading Inserts We can also add cascading entities to the database: Country spain = new Country(); spain.Name = "Spain"; spain.Population = "46 030 10"; spain.Cities.Add( new City { Name = "Barcelona"} ); spain.Cities.Add( new City { Name = "Madrid"} ); countryEntities.Countries.AddObject(spain); countryEntities.SaveChanges(); This way we dont have to add each City individually  They will be added when the Country entity (Spain) is inserted to the database 32
  33. 33. Updating Existing Data ObjectContext allows modifying entity properties and persisting them in the database  Just load an entity, modify it and call SaveChanges() The ObjectContext automatically tracks all changes made on its entity objects Order order = northwindEntities.Orders.First(); order.OrderDate = DateTime.Now; context.SaveChanges(); This will execute This will execute an SQL an SQL UPDATE SELECT to load the first order 33
  34. 34. Deleting Existing Data Delete is done by DeleteObject() on the specified entity collection SaveChanges() method performs the delete action in the database Order order = northwindEntities.Orders.First(); // Mark the entity for deleting on the next save northwindEntities.Orders.DeleteObject(order); northwindEntities.SaveChanges(); This will execute an SQL DELETE command 34
  35. 35. CRUD Operations with Entity Framework Live Demo
  36. 36. Executing Native SQL QueriesParameterless and Parameterized
  37. 37. Executing Native SQL Queries Executing a native SQL query in Entity Framework directly in its database store: ctx.ExecuteStoreQuery<return-type>(native-SQL-query); Example: string query = "SELECT count(*) FROM dbo.Customers"; var queryResult = ctx.ExecuteStoreQuery<int>(query); int customersCount = queryResult.FirstOrDefault(); Examples are shown in SQL Server but the same can be done for any other database 37
  38. 38. Executing Native SQL Queries (2) Native SQL queries can also be parameterized: NorthwindEntities context = new NorthwindEntities(); string nativeSQLQuery = "SELECT FirstName + + LastName " + "FROM dbo.Employees " + "WHERE Country = {0} AND City = {1}"; object[] parameters = { country, city }; var employees = context.ExecuteStoreQuery<string>( nativeSQLQuery, parameters); foreach (var emp in employees) { Console.WriteLine(emp); } 38
  39. 39. Executing Native SQL Queries Live Demo
  40. 40. The N+1 Query ProblemWhat is the N+1 Query Problem and How to Avoid It?
  41. 41. The N+1 Query Problem What is the N+1 Query Problem?  Imagine a database that contains tables Customers and Orders  A customer has multiple orders (one-to-many relationship)  We want to print each Customer and its Orders: foreach (var cust in context.Customers) { Console.WriteLine(cust.CompanyName + "nOrders:"); foreach (var order in cust.Orders) { Console.WriteLine("{0}", order.OrderID); } } 41
  42. 42. The N+1 Query Problem (2) A single query to retrieve the countries This code will execute N+1 DB queries: foreach (var cust in context.Customers) { Console.WriteLine(cust.CompanyName + "nOrders:"); foreach (var order in cust.Orders) { Console.WriteLine("{0}", order.OrderID); } } Additional N queries to retrieve the cities in each country Imagine we have 100 countries in the database  Thats 101 SQL queries  very slow!  We could do the same with a single SQL query 42
  43. 43. Solution to the N+1 Query Problem Fortunately there is an easy way in EF to avoid the N+1 query problem Using Include(…) method only one SQL query with join is made to get the child entities foreach (var country in countriesEntities.Countries.Include("Cities")) { foreach (var city in country.Cities) { Console.WriteLine(" {0}", city.CityName); } } No additional SQL queries are made here for the child entities 43
  44. 44. Solution to the N+1 Query Problem Live Demo
  45. 45. Joining andGrouping TablesJoin and Group Using LINQ
  46. 46. Joining Tables in EF In EF we can join tables in LINQ or by using extension methods on IEnumerable<T>  The same way like when joining collections northwindEntities.Customers. Join(northwindEntities.Suppliers, (c=>c.Country), (s=>s.Country), (c,s)=> new {Customer = c.CompanyName, Supplier = s.CompanyName, Country = c.Country }); var custSuppl = from customer in northwindEntities.Customers join supplier in northwindEntities.Suppliers on customer.Country equals supplier.Country select new { CustomerName = customer.CompanyName, Supplier = supplier.CompanyName, Country = customer.Country }; 46
  47. 47. Grouping Tables in EF Grouping also can be done by LINQ  The same ways as with collections in LINQ Grouping with LINQ: var groupedCustomers = from customer in northwindEntities.Customers group customer by Customer.Country; Grouping with extension methods: var groupedCustomers = northwindEntities.Customers.GroupBy( customer => customer.Country); 47
  48. 48. Joining andGrouping Tables Live Demo
  49. 49. Attaching andDetaching Objects
  50. 50. Attaching and Detaching Objects In Entity Framework, objects can be attached to or detached from an object context Attached objects are tracked and managed by the ObjectContext  SaveChanges() persists all changes in DB Detached objects are not referenced by the ObjectContext  Behave like a normal objects, like all others, which are not related to EF 50
  51. 51. Attaching Detached Objects When a query is executed inside an ObjectContext, the returned objects are automatically attached to it When a context is destroyed, all objects in it are automatically detached  E.g. in Web applications between the requests You might late attach to a new context objects that have been previously detached 51
  52. 52. Detaching Objects When an object is detached?  When we obtain the object from an ObjectContext and the Dispose it  Manually: by calling Detach(…) method Product GetProduct(int id) { using (NorthwindEntities northwindEntities = new NorthwindEntities()) { return northwindEntities.Products.First( p => p.ProductID == id); } } Now the returned product is detached 52
  53. 53. Attaching Objects When we want to update a detached object we need to reattach it and the update it  Done by the Attach(…) method of the context void UpdatePrice(Product product, decimal newPrice) { using (NorthwindEntities northwindEntities = new NorthwindEntities()) { northwindEntities.Products.Attach(product); product.UnitPrice = newPrice; northwindEntities.SaveChanges(); } } 53
  54. 54. Attaching andDetaching Objects Live Demo
  55. 55. Using Transactions in EF Just Use the TransactionScope Class
  56. 56. Using Transactions in EF To perform transactional logic, just use the TransactionScope class  You may need to add reference to System.Transactions.dll using (TransactionScope scope = new TransactionScope()) { NorthwindEntities context = new NorthwindEntities(); // Perform a series of changes in the context context.SaveChanges(); scope.Complete(); } 56
  57. 57. Transactions in EF Live Demo
  58. 58. ADO.NET Entity Framework курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
  59. 59. Exercises1. Using the Visual Studio Entity Framework designer create a ObjectContext for the Northwind database2. Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. Write a testing class.3. Write a method that finds all customers who have orders made in 1997 and shipped to Canada.4. Implement previous by using native SQL query and executing it through the ObjectContext.5. Write a method that finds all the sales by specified region and period (start / end dates). 59
  60. 60. Exercises (2)6. Create a database called NorthwindTwin with the same structure as Northwind using the features from ObjectContext. Find for the API for schema generation in MSDN or in Google.7. Try to open two different data contexts and perform concurrent changes on the same records. What will happen at SaveChanges()? How to deal with it?8. By inheriting the Employee entity class create a class which allows employees to access their corresponding territories as property of type EntitySet<T>. 60
  61. 61. Exercises (3)9. Create a method that places a new order in the Northwind database. The order should contain several order items. Use transaction to ensure the data consistency.10. Create a stored procedures in the Northwind database for finding the total incomes for given supplier name and period (start date, end date). Implement a C# method that calls the stored procedure and returns the retuned record set. 61
  62. 62. Exercises (4)11. Create a database holding users and groups. Create a transactional EF based method that creates an user and puts it in a group "Admins". In case the group "Admins" do not exist, create the group in the same transaction. If some of the operations fail (e.g. the username already exist), cancel the entire transaction. 62
  63. 63. Free Trainings @ Telerik Academy "C# and Databases " course @ Telerik Academy  academy.telerik.com/…csharp-databases Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com 63
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×