Mastering Entity Framework Core 2.0

Book description

Learn how to leverage the features of the new Entity Framework Core APIs and use them to build pure .NET Core applications.

About This Book

  • Learn how to effectively manage your database to make it more productive and maintainable.
  • Write simplified queries using LINQ to acquire the desired data easily
  • Raise the abstraction level from data to objects so teams can function independently, resulting in easily maintainable code

Who This Book Is For

This book is for .NET Core developers who would like to integrate EF Core in their application. Prior knowledge of .NET Core and C# is assumed.

What You Will Learn

  • Create databases and perform CRUD operations on them
  • Understand and build relationships (related to entities, keys, and properties)
  • Understand in-built, custom, and remote validation (both client and server side)
  • You will learn to handle concurrency to build responsive applications
  • You will handle transactions and multi-tenancy while also improving performance

In Detail

Being able to create and maintain data-oriented applications has become crucial in modern programming. This is why Microsoft came up with Entity Framework so architects can optimize storage requirements while also writing efficient and maintainable application code. This book is a comprehensive guide that will show how to utilize the power of the Entity Framework to build efficient .NET Core applications. It not only teaches all the fundamentals of Entity Framework Core but also demonstrates how to use it practically so you can implement it in your software development.

The book is divided into three modules. The first module focuses on building entities and relationships. Here you will also learn about different mapping techniques, which will help you choose the one best suited to your application design. Once you have understood the fundamentals of the Entity Framework, you will move on to learn about validation and querying in the second module. It will also teach you how to execute raw SQL queries and extend the Entity Framework to leverage Query Objects using the Query Object Pattern. The final module of the book focuses on performance optimization and managing the security of your application. You will learn to implement failsafe mechanisms using concurrency tokens. The book also explores row-level security and multitenant databases in detail.

By the end of the book, you will be proficient in implementing Entity Framework on your .NET Core applications.

Style and approach

This book is filled with various examples that will help you use Entity Framework Core 2.0 to write efficient software.

Table of contents

  1. 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. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Kickstart - Introduction to Entity Framework Core
    1. Prerequisites
    2. Creating a new project
      1. The Start page
      2. The File menu
      3. Structuring the web app
    3. Installing Entity Framework
    4. Data models
      1. Blog entity
        1. Post entity
    5. Database context
    6. Registering the context in services (.NET Core DI)
    7. Creating and seeding databases
    8. CRUD operations
    9. Summary
  3. The Other Way Around – Database First Approach
    1. Preparing the database
      1. Blog entity script
      2. Post entity script
    2. Creating  new project
      1. Installing Entity Framework
    3. Reverse engineering the database
      1. Configuring data context
      2. Working with the Blog entity
      3. Working with the Post entity
    4. Registering context in services (.NET Core DI)
      1. Refactoring the OnConfiguring() method
      2. Refactoring the ConfigureServices method
      3. The appsettings.json setting
    5. Performing CRUD operations
      1. Creating controller action
      2. Edit controller action
      3. The Delete view
    6. Summary
  4. Relationships – Terminology and Conventions
    1. Understanding relationship terms
      1. Data models
        1. Blog entity
        2. The Post entity
      2. Principal entity
      3. Principal key
      4. Dependent entity
      5. Foreign key
      6. Navigation property
        1. Collection navigation property
        2. Reference navigation property
        3. Inverse navigation property
    2. Conventions in a relationship
      1. Fully-defined relationships
        1. Fully-defined relationships - under the hood
      2. No foreign key property
        1. No foreign key property - under the hood
      3. Single navigation property
      4. Foreign key
      5. Inverse property
    3. Summary
  5. Building Relationships – Understanding Mapping
    1. Relationships
      1. The one-to-one relationship
        1. Building one-to-one relationships using the Fluent API
      2. The one-to-many relationship
      3. The many-to-many relationship
        1. Building many-to-many relationship using the Fluent API
    2. Fluent API
      1. Identifying navigation property and inverse navigation
      2. Identifying the single navigation property
      3. Relationship-building techniques
        1. Building relationship using a foreign key
        2. Building relationships using a principal key
        3. Building relationships using the IsRequired method
      4. Cascade delete
      5. Data migration issue with EF Core 2.0
    3. Summary
  6. Know the Validation – Explore Inbuilt Validations
    1. Diving into built-in validations
    2. Required field validation
      1. Incorporating the Required validation in blogging system models
    3. EmailAddress field validation
      1. Incorporating EmailAddress validation in blogging system models
    4. Compare field validation
      1. Incorporating the Compare validation in blogging system models
    5. Url field validation
      1. Incorporating Url validation in blogging system models
    6. MinLength field validation
    7. MaxLength field validation
    8. RegularExpression field validation
    9. Summary
  7. Save Yourself – Hack Proof Your Entities
    1. Client-side validation
    2. Validating data without client-side scripting
      1. Server-side validation
      2. Manual validation
    3. Custom validation
      1. Creating client-side logic for custom validation
    4. Remote validation
    5. Summary
  8. Going Raw – Leveraging SQL Queries in LINQ
    1. Basic raw SQL queries
    2. Building parameterized queries
    3. Composing with LINQ
    4. Executing SQL query without a DBSet or POCO
    5. Summary
  9. Query Is All We Need – Query Object Pattern
    1. Introduction to query objects
    2. Improving repositories with the query object pattern
      1. Introduction to repositories
        1. Solution to the repository assignment
      2. Incorporating the query object pattern into repositories
        1. List query object support in the repository
        2. Single query object support in the repository
        3. Solution to the repository with the queries assignment
    3. Composing queries with commands
      1. Solution to the command queries assignment
    4. Enhancing queries with expression trees
      1. Solution to the expression trees assignment
    5. Summary
  10. Fail Safe Mechanism – Transactions
    1. Default behavior of a transaction
      1. Adding tags support in the blogging system
      2. Leveraging default transaction behavior
    2. Creating a simple transaction
    3. Creating a cross-context transaction
      1. File upload support to the blogging system
      2. Limitations to the transaction scope
      3. Custom transaction scope support
      4. Adding date picker support to the blogging system
    4. Leveraging transactions between multiple technologies
      1. Recent posts support to the blogging system
      2. Anonymous posts list and individual blog post
      3. Listing comments
      4. Adding comments using external database transactions
    5. Summary
  11. Make It Real – Handling Concurrencies
    1. Handling concurrency in EF
      1. Understanding the concurrency conflicts
        1. Optimistic concurrency
        2. Pessimistic concurrency
    2. Introducing concurrency tokens
    3. Non-timestamp based concurrency tokens
      1. Configuring non-timestamp tokens through data annotation
      2. Configuring non-timestamp tokens through Fluent API
    4. Timestamp-based concurrency tokens
      1. Configuring timestamp tokens through data annotation
      2. Configuring timestamp tokens through Fluent API
    5. Handling concurrency conflicts
      1. Applying optimistic concurrency
        1. Database wins
        2. Client wins
        3. User-specific custom resolution
      2. Applying pessimistic concurrency
    6. Summary
  12. Performance – It's All About Execution Time
    1. The AsNoTracking() method
      1. How does tracking work?
        1. No-tracking queries
        2. Projections
    2. Detecting changes
    3. Asynchronous operations
      1. Transactions leveraging asynchronous operations
    4. Unnecessary volume returned
    5. The N+1 Select problem
    6. More data than required
    7. Mismatched data types
    8. Missing indexes
    9. Summary
  13. Isolation – Building a Multi-Tenant Database
    1. Authentication in the blogging system
    2. Row-Level Security
      1. Filter predicate
      2. Block predicate
    3. Multi-tenancy
      1. Standalone
      2. Database-per-tenant
      3. Shared multi-tenant
      4. Dive into multi-tenancy
      5. Blocking cross-tenant write operation
    4. Summary

Product information

  • Title: Mastering Entity Framework Core 2.0
  • Author(s): Prabhakaran Anbazhagan
  • Release date: December 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788294133