Presentation is loading. Please wait.

Presentation is loading. Please wait.

Entity Framework By: Casey Griffin.

Similar presentations


Presentation on theme: "Entity Framework By: Casey Griffin."— Presentation transcript:

1 Entity Framework By: Casey Griffin

2 What is Entity Framework?
Object-Relational Mapper Object Programming / Database Programming Create Model (Code/EF Designer) Part of the ADO.NET Stack – Between the applications and databases Higher Level of Data Abstraction Create/Maintain Data-Oriented Applications with Less Code Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Entity Framework allows you to create a model by writing code or using boxes and lines in the EF Designer. The Entity Framework is the preferred method of data access for .NET applications, supporting strongly-typed access through LINQ. The Entity Framework can also be used from an ASP.NET application through the Entity Data Source. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications

3 Conceptual Model Entity Data Model – EDM /Domain Model
Represents Domain Objects/ Relationships between Entities Reflects Application Logic Higher Level of Abstraction The Entity Framework allows developers to program against a conceptual model that reflects application logic rather than a relational model that reflects the database structure. EDM represents your domain objects and relationships between entities. Not tightly bound to data source or the application. Entities do not have to have a single table or data source.

4 Development Workflows
Four workflows – Are you targeting a new database or an existing database? Would you like to work with a model or with code?

5 Code First Approach Since Entity Framework 4.1 Workflow
Domain (POCO) Classes DbContext API Code First Migrations Code First lets you use your own classes as your data model and supports automatically creating a database or using an existing database.

6 POCO Classes Plain Old CLR Object Classes Persistent Ignorance
Types of Properties: Scalar Properties Navigational Properties Virtual Keyword (Lazy Loading) Domain name classes which have no dependence on Entity Framework – Persistent Ignorance. Scalar properties are like ID and Name and describe properties of the object. Navigational Properties describe the relationships between classes. Adding in the virtual Keyword allows for EF feature of Lazy Loading which is basically load these properties only when they are needed.

7 DbContext Lightweight version of the ObjectContext
Represent Database Session Querying and Saving Data DbSet (ObjectSet)– Query and Save Instances of Database Types Lightweight version of object context used to managing entities. The DbContext will handle the classes and pull them into Entity Framework. DbContext focuses on the most commonly used things people do with Entity Framework. The DbContext represents a session with the database and allows for selecting, inserting, updating, and deleting functions. CRUD operations. Define a DbSet for every type that is in the model and allows us to query and save instances of those database types The DbContext is a main object in Code First that exposes the interaction between the Entity Framework and the database. Its responsibilities include database connectivity, change tracking, data persistence, entities caching and more. It's also a container of all the DbSets that you'll create in order to work against the database. The DbSet is a collection representation of a set of entities. You can think about it as an in-memory representation of database records, but it's much more than that. It exposes a set of collection methods such as Add and Remove in order to manipulate the set's data. It also enables you to use LINQ to Entities to query its data.

8 Code First Conventions
Conventions determine the database schema based on the domain model Configuring/Overriding Conventions Data Annotations Fluent API EF Code First uses a set of conventions to work out what the database schema should be created based on how we defined out classes in the domain model. Determine the shape of classes and what the database will look like. There are times when your classes aren't going to be following what Code First is expecting or you want to change what the database looks like.

9 Data Annotations Data Annotations helps configure Code First Conventions within the POCO classes. Adding annotations such as key help to Code First Conventions to identify what the key of the class should be.

10 Fluent API To use Fluent API, you override the OnModelCreating method within the DbContext. You can then configure entities and mappings as you like.

11 Code First Migrations Enable Migrations Generating Seed Data
Adding Migrations Updating the Database Making changes to the domain model thus making changes to the database schema. Enable Migrations for the DbContext. Configuration file with seed data. Migrations keeps track of which migrations have been applied to the DbContext

12 The Griffin Drywall Project
The Website Demo

13 Analysis Comparison with Traditional Data Access
Entity Framework Features Benefits and Disadvantages

14 Features Conceptual Layer –Multiple Backends Lazy Loading
LINQ Querying Many to Many Relationships – Bridging Tables Conceptual Layer goes with persistent ignorance and can be placed on top of different data source providers without any or little coding changes. Lazy Loading enables us to only use resources when they are needed. LINQ Querying – Parameterized SQL statements prevents SQL injection. LINQ queries are against the conceptual model and therefore queries will not have to change with changes to the underlying database or physical model. LINQ uses caching so repetitive query will perform better

15 Benefits and Disadvantages
Bulk of 80-90% of CRUD Functions 10-20% SQL or Stored Procedures Rapid Development Speeds Not As Flexible as SQL/SP Maintainable Code Bad Generated SQL Isolated from Database EF used for most common CRUD Functions but bulk updates are better preformed with raw SQL or Stored Procedures EF will update conceptual model and physical/storage/database model as you would therefore keeping things in sync. Code is built around the conceptual model, the business logic and therefore queries are readable without knowing the database storage schema. SQL/SP can do more specific querying that EF cannot do eloquently

16 Conclusion Entity Framework is easy to use and endows developers with higher level of abstraction which allows them to deal with the business logic rather than the database logic. Future Projects Model First Approach EF vs Current ORMs

17 Questions Open the presentation up for questions.


Download ppt "Entity Framework By: Casey Griffin."

Similar presentations


Ads by Google