microsoft entity framework

of 20/20
Microsoft Entity Framework

Post on 22-May-2015




2 download

Embed Size (px)


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


  • 1. Microsoft Entity Framework

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. 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 doesnt need to write it manually 4. ORM tools ADO.NET Entity Framework, Microsofts ORM (released with .NET 3.5 SP1)Base One Foundation Component Library, free or commercialBCSEi ORM Code Generator, free or commercialBusiness Logic Toolkit for .NET, open sourceCastle ActiveRecord, ActiveRecord for .NET, open sourceDataObjects.Net v4.0, open source, commercialDevForce, commercial, N-TierDeveloper Express, eXpress Persistent Objects (XPO)EntitySpaces, commercial 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. Why Entity Framework Domain model pattern, using this pattern means that you model your entities based on your business conceptsrather 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. Entity Framework vs. Linq To SQL Entity FrameworkLinQ to SQL (L2S)Full provider model, supports DB2, Oracle, MySQL, SQLLimited provider modelModel entities can map to one or more tables using table per type or table per inheritanceOne entity only maps to a single tableContinuous supportMicrosoft is no longer supports itMultiple modeling techniquesOnly single model technique 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. 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 nuggetType get-package -remote -filter entityframework then type install-package -id EntityFramework use data annotations to specify properties at the target [email protected] 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 [email protected] 11. Performing CRUD [email protected] 12. Query 1. Linq To Entities 2.Entity [email protected] 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("Select * from Student where StudentName = 'StudentName1'", null).ToList(); }[email protected] 14. Update [email protected] 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 [email protected] 16. Add new [email protected] 17. Delete [email protected] 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 [email protected] 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. [email protected] 20. EF performance Tuning 1. Efficiently load related data 2. Use NoTracking merge option 3. Pre-Compile linQ queries private static readonly Func> compiledInstructorNamesQuery = CompiledQuery.Compile((SchoolEntities context) => from i in context.InstructorNames orderby i.FullName select i);1. Examine query commands sent to the [email protected]