Microsoft Entity Framework


Published on

Microsoft Entity framework is one of the best ORM tools that facilitates the data access.

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • In Slide Show mode, click the arrow to enter the PowerPoint Getting Started Center.
  • Microsoft Entity Framework

    1. 1. Microsoft Entity Framework
    2. 2. Microsoft Entity Framework • Microsoft Entity framework is one of the best object relational mapping frameworks. • Facilitates accessing databases and other relational data sources rather than the other frameworks. • The current version is EF 6.0 (see
    3. 3. Object Relational Mapping (ORM) It is a tool for storing data from domain objects to relational database like MS SQL Server in an automated way without much programming. O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information on how domain objects maps to relational database objects (tables, views & stored procedures). ORM helps us to keep our database design separate from our domain class design. This makes application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so developer doesn’t need to write it manually
    4. 4. ORM tools  ADO.NET Entity Framework, Microsoft’s ORM (released with .NET 3.5 SP1)  Base One Foundation Component Library, free or commercial  BCSEi ORM Code Generator, free or commercial  Business Logic Toolkit for .NET, open source  Castle ActiveRecord, ActiveRecord for .NET, open source  DataObjects.Net v4.0, open source, commercial  DevForce, commercial, N-Tier  Developer Express, eXpress Persistent Objects (XPO)  EntitySpaces, commercial
    5. 5. ORM Tools (Cont.)                        Euss, open source Habanero, Free open source BATIS, Free open source Invist,free ORM and code generation tool LLBLGen, open source drivers, commercial LightSpeed, free or commercial Neo, open source NConstruct, commercial NHibernate, open source Opf3, free and commercial ObjectMapper .NET, GPL and commercial license OpenAccess, free or commercial TierDeveloper, free ORM and code generation tool Persistor.NET, free or commercial Quick Objects, free or commercial Sooda, open source; BSD license Subsonic, open source Orasis, free trial or buy. Telerik, express or buy. CSLA.NET, free. ECO, free or commercial nhydrate, open source .netTiers, open source
    6. 6. Why Entity Framework  Domain model pattern, using this pattern means that you model your entities based on your business concepts rather than the database layer. Provides mapping from logical layer to the physical layer.  Huge code reduction, allows developers to focus on the business logic and providing automated code for storing and accessing the database  There is no SQL update, insert, delete statements written in different layers of the application.  Rich query capability, by providing rich object oriented query language.  Navigation. You can navigate object relationships transparently.  Concurrency, allowing multiple users updating the same data simultaneously.
    7. 7. Entity Framework vs. Linq To SQL Entity Framework LinQ to SQL (L2S) Full provider model, supports DB2, Oracle, MySQL, SQL Limited provider model Model entities can map to one or more tables using table per type or table per inheritance One entity only maps to a single table Continuous support Microsoft is no longer supports it Multiple modeling techniques Only single model technique
    8. 8. Entity Framework Architecture 1. 2. 3. 4. 5. Entity Data Model Linq to Entities Entity SQL Object Service “The entry point for accessing database and returning data to it” Entity Client data provider “Responsible for converting L2E and Entity SQL queries to database queries” 6. ADO.NET data provider “this layer communicates directly with the database using standard ADO.NET”
    9. 9. EF Code First There are two new types introduced for Code First approach, DbContext and DbSet. DbContext is a simplified alternative to ObjectContext and is the primary object for interacting with a database using a specific model. DbSet(Of TEntity) is a simplified alternative to ObjectSet(Of TEntity) and is used to perform CRUD operations against a specific type from the model in Code First approach. • To include the entity framework in your project using nugget Type get-package -remote -filter entityframework then type install-package -id EntityFramework • use data annotations to specify properties at the target database.
    10. 10. Code first to an existing database Install entity framework power tools from visual studio gallery Just right click the project and select reverse engineer Code First
    11. 11. Performing CRUD operations
    12. 12. Query 1. Linq To Entities 2. Entity SQL
    13. 13. Query 3. Native SQL //Querying with native sql using (SchoolDBEntities ctx = new SchoolDBEntities()) { //Inserting Student using ExecuteStoreCommand int InsertedRows = ctx.ExecuteStoreCommand("Insert into Student(StudentName,StandardId) values('StudentName1',1)"); //Fetching student using ExecuteStoreQuery var student = ctx.ExecuteStoreQuery<Student>("Select * from Student where StudentName = 'StudentName1'", null).ToList(); }
    14. 14. Update entities
    15. 15. Self Tracking Entities There is common problem when using entity framework with multitier application, entities lose states outside the scope of an object context, Solution: Self tracking entities
    16. 16. Add new entities
    17. 17. Delete Entities
    18. 18. Concurrency A concurrency conflict occurs when one user displays an entity's data in order to edit it, and then another user updates the same entity's data before the first user's change is written to the database. If you don't enable the detection of such conflicts, whoever updates the database last overwrites the other user's changes. There are two types of concurrency 1. Pessimistic Concurrency (Locking) 2. Optimistic Concurrency
    19. 19. Transactions We use transactions when multiple calls of SaveChanges are called or when multiple operations are performed on a single entity object. There are two approaches for performing Transactions 1. DbTransaction 2. TransactionScope
    20. 20. EF performance Tuning 1. Efficiently load related data 2. Use NoTracking merge option 3. Pre-Compile linQ queries private static readonly Func<SchoolEntities, IQueryable<InstructorName>> compiledInstructorNamesQuery = CompiledQuery.Compile((SchoolEntities context) => from i in context.InstructorNames orderby i.FullName select i); 1. Examine query commands sent to the database.