O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Entity Framework Core Cookbook - Second Edition

Book Description

Leverage the full potential of Entity Framework with this collection of powerful and easy-to-follow recipes

About This Book

  • Learn how to use the new features of Entity Framework Core 1
  • Improve your queries by leveraging some of the advanced features
  • Avoid common pitfalls
  • Make the best of your .NET APIs by integrating with Entity Framework

Who This Book Is For

This book is for .NET developers who work with relational databases on a daily basis and understand the basics of Entity Framework, but now want to use it in a more efficient manner. You are expected to have some prior knowledge of Entity Framework.

What You Will Learn

  • Master the technique of using sequence key generators
  • Validate groups of entities that are to be saved / updated
  • Improve MVC applications that cover applications developed using ASP.NET MVC Core 1
  • Retrieve database information (table, column names, and so on) for entities
  • Discover optimistic concurrency control and pessimistic concurrency control.
  • Implement Multilatency on the data side of things.
  • Enhance the performance and/or scalability of Entity Framework Core
  • Explore and overcome the pitfalls of Entity Framework Core

In Detail

Entity Framework is a highly recommended Object Relation Mapping tool used to build complex systems. In order to survive in this growing market, the knowledge of a framework that helps provide easy access to databases, that is, Entity Framework has become a necessity. This book will provide .NET developers with this knowledge and guide them through working efficiently with data using Entity Framework Core.

You will start off by learning how to efficiently use Entity Framework in practical situations. You will gain a deep understanding of mapping properties and find out how to handle validation in Entity Framework. The book will then explain how to work with transactions and stored procedures along with improving Entity Framework using query libraries. Moving on, you will learn to improve complex query scenarios and implement transaction and concurrency control. You will then be taught to improve and develop Entity Framework in complex business scenarios. With the concluding chapter on performance and scalability, this book will get you ready to use Entity Framework proficiently.

Style and approach

Filled with rich code-based examples, this book takes a recipe-based approach that will teach .NET developers to improve their understanding of Entity Framework and help them effortlessly apply this knowledge in everyday situations.

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 code file.

Table of Contents

  1. Entity Framework Core Cookbook - Second Edition
    1. Table of Contents
    2. Entity Framework Core Cookbook - Second Edition
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
      8. Downloading the example code
        1. Errata
        2. Piracy
        3. Questions
    8. 1. Improving Entity Framework in the Real World
      1. Introduction
      2. Improving Entity Framework by using a code-first approach
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Convention over configuration
          2. Model-View-Controller
          3. Single Responsibility Principle
          4. Provider Model
          5. Testing
        5. See also
      3. Unit testing and mocking
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. One class under test
          2. Integration tests
          3. Arrange, Act, Assert
          4. Mocking
      4. Creating databases from code
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Creating mock database connections
        1. How to do it…
        2. How it works…
        3. See also
      6. Implementing the repository pattern
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Dependency Inversion Principle
          2. Repository and caching
          3. Mocking
          4. Where generic constraint
        5. See also
      7. Implementing the unit of work pattern
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Call per change
          2. Interface Segregation Principle
          3. Refactoring
        5. See also
    9. 2. Mapping Entities
      1. Introduction
      2. Mapping non-public members
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Mapping interfaces
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Shadow properties
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Creating one-to-one maps
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Creating one-to-many maps
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      7. Creating many-to-many maps
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      8. Creating custom conventions
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Using sequence key generators
        1. Getting ready
        2. How to do it…
        3. How it works...
        4. There's more…
        5. See also
      10. Using GUIDs as keys
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      11. Implementing inheritance – Table per Class hierarchy
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    10. 3. Validation and Changes
      1. Introduction
      2. Validating simple properties
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Validating the whole entity
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Validating groups of entities
        1. How to do it…
        2. How it works…
        3. See also
      5. Intercepting saving changes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      6. Intercepting property changes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Setting the state of an entity
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      8. Improving MVC UI with entity framework validation
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Understanding the HTML helper
        5. See also
      9. Inserting, updating, and deleting entities with stored procedures
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      10. Updating the database from model changes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      11. Dumping the SQL script for the database creation
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
    11. 4. Transactions and Concurrency Control
      1. Introduction
      2. Using explicit transactions
        1. Atomic
        2. Consistent
        3. Isolation
        4. Durability
        5. Getting ready
        6. How to do it…
        7. How it works…
        8. See also
      3. Using transactions in custom SQL operations
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Implementing optimistic concurrency in SQL Server
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Database wins
          2. Client wins/last one wins
        5. See also
      5. Implementing optimistic concurrency in a database-agnostic way
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
    12. 5. Querying
      1. Introduction
      2. Executing client-side functions in LINQ queries
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      3. Mixing SQL with LINQ queries
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Getting entities from the local cache
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Creating filtered collections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Creating reusable queries
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Extension methods
          2. Naming conflict
        5. See also
      7. Querying shadow properties
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      8. Implementing the query object pattern
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Using dynamic LINQ
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    13. 6. Advanced Scenarios
      1. Introduction
      2. Generating entities from the database
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      3. Implementing multitenancy
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      4. Strongly typed bulk operations
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      5. Handling soft deletes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      6. Adding logging
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      7. Capturing the audit data
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      8. Retrieving entity metadata
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Improving MVC applications
        1. Getting ready
        2. How to do it.…
        3. See also
      10. Hooking infrastructure services
      11. Using other databases
        1. How to do it…
    14. 7. Performance and Scalability
      1. Introduction
      2. Improving the performance of queries
        1. Getting ready
        2. How to do it…
          1. Modifications
            1. Updates
            2. Deletes
          2. Queries
            1. SQL
            2. LINQ
        3. See also
      3. Testing and profiling queries
        1. How to do it…
          1. Logging
          2. Database profiler
          3. Real time monitoring
          4. Testing
        2. See also
      4. Using asynchronous operations
        1. Getting ready
        2. How to do it…
          1. Queries
          2. Modifications
        3. See also
      5. Eager loading
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      6. Using the cache
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    15. A. Pitfalls
      1. Introduction
      2. GroupBy executes on the client side
        1. Problem
        2. How to fix it…
      3. Table per class hierarchy requires nullable columns for derived classes
        1. Problem
        2. How to solve it…
      4. References not eagerly fetched are lost
        1. Problem
        2. How to solve it…
      5. Date/time operations are not supported
        1. Problem
        2. How to solve it…
      6. Paging in SQL Server earlier than 2012
        1. Problem
        2. How to solve it…
      7. Database null semantics
        1. Problem
        2. How to solve it…
      8. Migrations and contexts with parameterized constructors
        1. Problem
        2. How to solve it…
      9. Migrations with contexts in different projects
        1. Problem
        2. How to solve it…
      10. Accessing the service provider too soon
        1. Problem
        2. How to solve it…
      11. Setting the maximum string length
        1. Problem
        2. How to solve it…
      12. Mapping discriminator columns
        1. Problem
        2. How to solve it…
      13. Composite primary keys
        1. Problem
        2. How to solve it…
      14. Refreshing entities
        1. Problem
        2. How to solve it…
      15. Cascading entity deletes
        1. Problem
        2. How to solve it…
    16. Index