You are previewing Mastering Entity Framework.
O'Reilly logo
Mastering Entity Framework

Book Description

Effortlessly produce data-driven applications for .NET to address the competing demands of data storage and data modeling with Entity Framework

In Detail

Data access is an integral part of any software application. Entity Framework provides a model-based system that makes data access effortless for developers by freeing you from writing similar data access code for all of your domain models.

Mastering Entity Framework provides you with a range of options when developing a data-oriented application. You’ll get started by managing the database relationships as Entity relationships and perform domain modeling using Entity Framework. You will then explore how you can reuse data access layer code such as stored procedures and table-valued functions, and perform various typical activities such as validations and error handling. You’ll learn how to retrieve data by querying the Entity Data Model and understand how to use LINQ to Entities and Entity SQL to query the Entity Data Model.

What You Will Learn

  • Find out the benefits of using Entity Framework over plain ADO.NET

  • Discover various approaches of using Entity Framework such as Database First, Model First, and Code First, and also learn how to choose the best approach for a given scenario

  • Explore how to manage Entity relationships, implement Entity inheritance, and perform model validations

  • Understand how to use stored procedures and functions using Entity Framework

  • Familiarize yourself with domain modeling using Entity Framework

  • Become proficient in managing database creation and seeding dummy data in a database

  • Understand LINQ to Entities and Entity SQL to query data using Entity Framework

  • Manage concurrency and transactions using Entity Framework

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mastering Entity Framework
      1. Table of Contents
      2. Mastering Entity Framework
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
          3. Instant updates on new Packt books
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Introduction to Entity Framework
        1. Entity Framework modeling and persistence
          1. Understanding the Entity Data Model
          2. Understanding the ObjectContext class
        2. Development styles and different Entity Framework approaches
        3. Comparing the development styles
          1. The Database First approach
          2. The Model First approach
          3. The Code First approach
        4. Entity Framework Database First approach
        5. Entity Framework Model First approach
        6. Entity Framework Code First approach
        7. Performing CRUD operations using Entity Framework
          1. Reading a list of items
          2. Reading a specific item
          3. Creating a new item
          4. Updating an existing item
          5. Deleting an item
        8. Choosing persistence approaches
        9. Summary
      9. 2. Entity Framework DB First – Managing Entity Relationships
        1. Understanding database relationships
          1. One-to-many relationship
          2. One-to-one relationship
          3. Many-to-many relationship
        2. Creating the Entity model
        3. Modeling a one-to-many relationship
        4. Modeling a one-to-one relationship using Entity Framework
        5. Modeling a many-to-many relationship using Entity Framework
        6. Using navigation properties for data access
          1. Retrieving a specific item
          2. Retrieving a list of items
          3. Adding an item
          4. Updating an item
          5. Deleting an item
        7. Entity Framework – behind the scenes
        8. Summary
      10. 3. Entity Framework DB First – Performing Model Validations
        1. Model validations using Entity Framework
          1. Model validations using partial class methods
            1. Understanding partial methods
            2. Using partial methods to perform model validations
        2. Model validations using data annotations
          1. Specifying validation rules using data annotations
          2. Validating the required fields
            1. Validating the length of fields
            2. Regular expression-based validations
          3. Triggering validations using data annotations
            1. Trigger validations in data binding environments
            2. Trigger validations in non-data binding environments
            3. Implementing custom validations using data annotations
        3. Summary
      11. 4. Entity Framework DB First – Inheritance Relationships between Entities
        1. Domain modeling using inheritance in Entity Framework
        2. The Table per Type inheritance
          1. Generating the default Entity Data Model
          2. Deleting default relationships
          3. Adding inheritance relationships between entities
          4. Using the entities via the DBContext object
        3. The Table per Class Hierarchy inheritance
          1. Generating the default Entity Data Model
          2. Adding concrete classes to the Entity Data Model
          3. Mapping the concrete class properties to the respective tables and columns
          4. Making the base class entity abstract
          5. Using the entities via the DBContext object
        4. The Table per Concrete Class inheritance
          1. Generating the default Entity Data Model
          2. Creating the abstract class
          3. Modifying the CDSL to cater to the change
          4. Specifying the mapping to implement the TPT inheritance
          5. Using the entities via the DBContext object
        5. Choosing the inheritance strategy
        6. Summary
      12. 5. Entity Framework DB First – Using Views, Stored Procedures, and Functions
        1. Using views, procedures, and functions
        2. Using Entity Framework with views
        3. Using Entity Framework with stored procedures
          1. Defining stored procedures
        4. Using Entity Framework with functions
          1. Using scalar functions
          2. Using table valued functions
        5. Summary
      13. 6. Entity Framework Code First – Domain Modeling and Managing Entity Relationships
        1. Understanding the Entity Framework Code First approach
        2. Understanding the Code First conventions and configurations
        3. Implementing Entity Framework Code First
          1. More on domain class configurations
        4. Managing Entity relationships using the Code First approach
          1. Implementing one-to-many relationships
          2. Implementing one-to-one relationships
          3. Implementing many-to-many relationships
        5. Inheritance with the Entity Framework Code First approach
          1. Implementing the TPT inheritance
          2. Implementing the TPH inheritance
          3. Implementing the TPC inheritance
        6. Summary
      14. 7. Entity Framework Code First – Managing Database Creation and Seeding Data
        1. Managing database connections
          1. Managing connections using a configuration file
          2. Using the existing ConnectionString
          3. Using an existing connection
        2. Managing database initialization
          1. Setting the initialization strategy
        3. Seeding data
        4. Summary
      15. 8. Querying the Entity Data Model – LINQ to Entities
        1. Understanding LINQ to Entities
        2. Querying data using LINQ to Entities
        3. Using LINQ to Entities – an example-based approach
          1. Executing simple queries
          2. Using the navigation properties with LINQ to Entities
          3. Filtering data using LINQ to Entities
          4. Using LINQ projections with LINQ to Entities
          5. Grouping using LINQ to Entities
          6. Ordering using LINQ to Entities
          7. Aggregate operators with LINQ to Entities
            1. Count
            2. Sum
            3. Min
            4. Max
            5. Average
          8. Partitioning/paging data using LINQ to Entities
            1. Skip
            2. Take
            3. Implementing paging
          9. Implementing join using LINQ to Entities
        4. Lazy loading and eager loading
          1. Lazy loading
          2. Eager loading
        5. Summary
      16. 9. Querying the Object Model – Entity SQL
        1. Understanding Entity SQL
        2. Understanding EntityConnection
          1. Entity SQL with ObjectQuery
          2. Querying data using Entity SQL with ObjectQuery
          3. Executing parameterized Entity SQL with ObjectQuery
          4. Navigation properties using Entity SQL with ObjectQuery
          5. Aggregate functions with Entity SQL using ObjectQuery
            1. Count
            2. Sum
            3. Min
            4. Max
            5. Average
          6. Ordering data with Entity SQL using ObjectQuery
          7. Grouping data using Entity SQL with ObjectQuery
          8. Partitioning/paging data using Entity SQL ObjectQuery
            1. Skip
            2. Limit
            3. Implementing paging
        3. Using Entity SQL with EntityCommand
          1. Querying data using Entity SQL with EntityCommand
          2. Parameterized Entity SQL with EntityCommand
        4. Summary
      17. 10. Managing Concurrency Using Entity Framework
        1. Understanding concurrency
          1. Understanding optimistic concurrency
            1. Ignore the conflict/forcing updates
            2. Partial updates
            3. Warn/ask the user
            4. Reject the changes
          2. Understanding pessimistic concurrency
        2. Implementing optimistic concurrency using Entity Framework
          1. Entity Framework's default concurrency
          2. Designing applications to handle field level concurrency
          3. Implementing field level concurrency
          4. Implementing RowVersion for concurrency
        3. Entity Framework and pessimistic concurrency
        4. Summary
      18. 11. Managing Transactions Using Entity Framework
        1. Understanding transactions
        2. Setting up the test environment
        3. Entity Framework's default transaction handling
          1. Using TransactionScope to handle transactions
        4. Managing transactions using Entity Framework 6
          1. Using an existing transaction
        5. Choosing the appropriate transaction management
        6. Summary
      19. 12. Implementing a Small Blogging Platform Using Entity Framework
        1. Understanding the application requirements
        2. Visualizing our database design
        3. Creating the Entity Data Model
          1. Creating the entity classes
            1. The User entity
            2. The Role entity
            3. The Category entity
            4. The Blog entity
            5. The Comment entity
          2. Creating relationships and navigation properties
            1. The User entity
            2. The Role entity
            3. The Category entity
            4. The Blog entity
            5. The Comment entity
          3. Implementing the DbContext class
        4. Performing data access
          1. Understanding the Repository pattern
          2. Understanding Unit of Work
          3. Managing categories
            1. Listing categories
            2. Adding a category
            3. Updating a category
            4. Deleting a category
          4. Managing blogs
            1. Adding a new blog
            2. Updating a blog
            3. Deleting a blog
            4. Listing blogs on the home page
            5. Showing a single blog
          5. Managing comments
            1. Listing categories
            2. Adding a comment
            3. Deleting a comment
        5. Using other Entity Framework approaches
        6. Summary
      20. Index