You are previewing Programming Entity Framework: Code First.
O'Reilly logo
Programming Entity Framework: Code First

Book Description

This mini book will act as an extension to Programming Entity Framework 2nd Edition. Code First is an additional means of building a model to be used with the Entity Framework and is creating a lot of excitement in the .NET development community. The reader will begin with an overview of what Code First is, why it was created, how it fits into the Entity Framework and when to use it over the alternatives. Next the reader will gain an understanding of how Code First's built in conventions work and how to override them with configuration — whether they choose to use attribute based Data Annotations or strongly typed Fluent API configurations. The mini-book will then review the modeling possibilities of Code First, looking first at the impact of its conventions and then demonstrating how to override that behavior (and its impact on the database) first with the Annotations and then with the Fluent API. The book will also explore how Code First Database initialization works, the options it provides and how they impact your application development and database. Finally the reader will get a chance to see Code First in action in a small application. Because more complex application development with Code First will depend on an understanding of the DbContext API, which is the focus of Minibook #2, readers will see additional application scenarios with Code First in that following mini-book.

Table of Contents

  1. Programming Entity Framework: Code First
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. Preface
      1. Audience
      2. Contents of This Book
      3. Conventions Used in This Book
      4. Using Code Examples
      5. Safari® Books Online
      6. How to Contact Us
      7. Acknowledgments
    3. 1. Welcome to Code First
      1. Modeling with EF Before Code First
      2. Inception of Code First
      3. Getting Code First to Developers in Between .NET Releases
      4. Writing the Code…First
      5. Managing Objects with DbContext
      6. Using the Data Layer and Domain Classes
      7. Getting from Classes to a Database
      8. Working with Configuration
        1. Configuring with Data Annotations
        2. Configuring with the Fluent API
      9. Creating or Pointing to a Database
      10. What Code First Does Not Support
        1. Choosing Code First
      11. Learning from This Book
    4. 2. Your First Look at Code First
      1. Introducing EF to the Domain Classes
      2. Putting the Pieces Together in an Example
        1. Convention for Table, Schema, and Column Names
        2. Convention for Keys
        3. Convention for String Properties
        4. Convention for Byte Array
        5. Convention for Booleans
        6. Convention for One-to-Many Relationships
      3. Overriding Convention with Configurations
        1. Configuring with Data Annotations
      4. Understanding How Model Changes Impact Database Initialization
        1. Data Annotations and Validation-Aware UIs
      5. Configuring Code First with the Fluent API
        1. Organizing Fluent Configurations
      6. Summary
    5. 3. Using Conventions and Configurations for Property Attributes
      1. Working with Property Attributes in Code First
        1. Length
        2. Data Type
        3. Nullability and the Required Configuration
      2. Mapping Keys
        1. Code First Convention Response to Unconventional Key Properties
        2. Configuring the Key with Data Annotations
        3. Using HasKey to Configure a Key Property in the Fluent API
      3. Configuring Database-Generated Properties
        1. Configuring Database-Generated Options with Data Annotations
        2. Configuring Database-Generated Options with the Fluent API
      4. Configuring TimeStamp/RowVersion Fields for Optimistic Concurrency
        1. Code First Convention and TimeStamp fields
        2. Using Data Annotations to Configure TimeStamp
        3. Configuring TimeStamp/RowVersion with Fluent API
      5. Configuring Non-Timestamp Fields for Concurrency
        1. Configuring for Optimistic Concurrency with Data Annotations
        2. Configuring for Optimistic Concurrency with Fluent API
      6. Mapping to Non-Unicode Database Types
      7. Affecting the Precision and Scale of Decimals
        1. Convention for Precision and Scale
        2. Data Annotations for Precision and Scale
        3. Fluent Configuration for Precision and Scale
      8. Working with Complex Types in Code First
        1. Defining Complex Types by Convention
        2. Configuring Unconventional Complex Types
          1. Specifying complex types with Data Annotations
          2. Specifying complex types with the Fluent API
        3. Working with More Complicated Complex Types
      9. Configuring Properties of Complex Types
        1. Configuring Complex Types with Data Annotations
        2. Configuring Complex Type Properties with the Fluent API
      10. Summary
    6. 4. Using Convention and Configuration for Relationships
      1. Working with Multiplicity
        1. Configuring Multiplicity with Data Annotations
        2. Configuring Multiplicity with the Fluent API
      2. Working with Foreign Keys
        1. Specifying Unconventionally Named Foreign Keys
          1. Fixing foreign key with Data Annotations
          2. Fixing foreign key with the Fluent API
      3. Working with Inverse Navigation Properties
      4. Working with Cascade Delete
        1. Turning On or Off Client-Side Cascade Delete with Fluent Configurations
        2. Setting Cascade Delete Off in Scenarios That Are Not Supported by the Database
      5. Exploring Many-to-Many Relationships
      6. Working with Relationships that Have Unidirectional Navigation
      7. Working with One-to-One Relationships
        1. Configuring One-to-One Relationships When Both Ends Are Required
          1. Ensuring that the sample code honors the required Photo
          2. Configuring one-to-one with the Fluent API
      8. Summary
    7. 5. Using Conventions and Configurations for Database Mappings
      1. Mapping Class Name to Database Table and Schema Name
        1. Configuring Table and Schema Name with Data Annotations
        2. Configuring Table and Schema Name with the Fluent API
      2. Mapping Property Names to Database Columns
        1. Modifying the Default Column Name with Data Annotations
        2. Modifying the Default Column Name with the Fluent API
        3. Affecting Column Names for Complex Types
      3. Allowing Multiple Entities to Map to a Single Table: aka Table Splitting
        1. Mapping to a Common Table using Data Annotations
        2. Splitting a Table Using the Fluent API
      4. Mapping a Single Entity Across Multiple Tables
      5. Controlling Which Types Get Mapped to the Database
        1. Preventing Types from Being Included in the Model
          1. Using Data Annotations to ignore types
          2. Using Fluent Configuration to ignore types
      6. Understanding Property Mapping and Accessibility
        1. Scalar Property Mapping
        2. Accessibility of Properties, Getters, and Setters
      7. Preventing Properties from Being Included in the Model
        1. Data Annotations for Ignoring a Property
        2. Fluent Configuration for Ignoring a Property
      8. Mapping Inheritance Hierarchies
        1. Working with Code First’s Default Inheritance: Table Per Hierarchy (TPH)
        2. Customizing the TPH Discriminator Field with the Fluent API
        3. Configuring Table Per Type (TPT) Hierarchy
        4. Configuring for Table Per Concrete Type (TPC) Inheritance
        5. Avoiding Mapping Exceptions with TPC
      9. Working with Abstract Base Classes
      10. Mapping Relationships
        1. Controlling Foreign Keys Included in Your Class
        2. Controlling Foreign Keys That Are Created by Code First
          1. Controlling generated foreign keys with entity splitting
        3. Controlling Many-to-Many Join Tables
      11. Summary
    8. 6. Controlling Database Location, Creation Process, and Seed Data
      1. Controlling the Database Location
        1. Controlling Database Location with a Configuration File
        2. Controlling Database Name with DbContext Constructor
        3. Controlling Connection String Name with DbContext Constructor
        4. Reusing Database Connections
        5. Controlling Database Location with Connection Factories
          1. Working with SqlConnectionFactory
          2. Working with SqlCeConnectionFactory
          3. Writing a custom connection factory
      2. Working with Database Initialization
        1. Controlling When Database Initialization Occurs
        2. Switching Off Database Initialization Completely
        3. Database Initializers Included in Entity Framework
        4. Creating a Custom Database Initializer
        5. Setting Database Initializers from a Configuration File
      3. Using Database Initializers to Seed Data
      4. Using Database Initialization to Further Affect Database Schema
      5. Summary
    9. 7. Advanced Concepts
      1. Mapping to Nontable Database Objects
        1. Mapping to Updatable Views
        2. Using Views to Populate Objects
        3. Using Views to Populate Nonmodel Objects
        4. Working with Stored Procedures
      2. Removing Conventions
      3. Taking Control of Model Caching
        1. Understanding Model Caching
        2. Overriding Default Model Caching
      4. Working with the EdmMetadata Table
        1. Coding Against EdmMetadata
        2. Preventing Code First from Creating and Seeking EdmMetadata
      5. Using Code First with ObjectContext
      6. Summary
    10. 8. What’s Coming Next for Code First
      1. Code First Migrations
      2. Entity Framework Power Tools
        1. Reverse Engineer Code First
        2. Viewing a Code First Model
          1. View Entity Data Model (Read-only)
          2. View Entity Data Model XML
          3. View Entity Data Model DDL SQL
        3. Optimize Entity Data Model
    11. About the Authors
    12. SPECIAL OFFER: Upgrade this ebook with O’Reilly