microsoft entity framework
DESCRIPTION
Microsoft Entity framework is one of the best ORM tools that facilitates the data access.TRANSCRIPT
MicrosoftEntity Framework
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 http://msdn.microsoft.com/en-us/data/jj574253.aspx)
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
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
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
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.
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
Entity Framework Architecture
1. Entity Data Model2. Linq to Entities3. Entity SQL4. Object Service “The entry point for accessing database and returning data to it”5. 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”
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.
Code first to an existing database
Install entity framework power tools from visual studio gallery
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
Just right click the project and select reverse engineer Code First
Performing CRUD operations
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(); }
Update entities
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
Add new entities
Delete Entities
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
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
EF performance Tuning
1. Efficiently load related data
2. Use NoTracking merge option
3. Pre-Compile linQ queries
4. Examine query commands sent to the database.
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);